Returns the current database connection from $db. The database connection in use Return type $data = $builder -> get () -> getResultArray () /* * Would execute and return an array of results of the following query: * SELECT field1, field2 FROM mytable WHERE field3 = 5 */ Class Reference class CodeIgniter\Database\ BaseBuilder db ( ) Returns like add it to a cron script for // later execution or something. select () -> where ( 'field3', 5 ) -> getCompiledSelect ( false ) //. setData ( $data ) -> onConstraint ( 'title, author' ) -> updateBatch () // OR $builder -> setData ( $data, null, 'u' ) -> onConstraint () -> updateBatch () // OR foreach ( $data as $row ) $builder -> onConstraint ( 'title, author' ) -> updateBatch () // OR $builder -> setData ( $data, true, 'u' ) -> onConstraint ( new RawSql ( '`mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author`' )) -> updateFields (, true ) -> updateBatch () /* * Produces: * UPDATE `mytable` * INNER JOIN ( * SELECT 'Title 1' `title`, 'Author 1' `author`, 'Name 1' `name`, 'Date 1' `date` UNION ALL * SELECT 'Title 2' `title`, 'Author 2' `author`, 'Name 2' `name`, 'Date 2' `date` * ) `u` * ON `mytable`.`title` = `u`.`title` AND `mytable`.`author` = `u`.`author` * SET * `mytable`.`title` = `u`.`title`, * `mytable`.`name` = `u`.`name`, * `mytable`.`date` = `u`.`date`, * `mytable`.`last_update` = CURRENT_TIMESTAMP() // this only applies to the last scenario */ Logics with different combinations of select(), update(), In our case, it will save you from the need to implement complex Standard for (optional) DELETE + INSERT, using PRIMARY and UNIQUE This method executes a REPLACE statement, which is basically the SQL Updating Data Update $builder->replace() Notice that the updated_at field is not inserted but is used on update. 2, 'email' => ' ', 'name' => 'Ahmadinejad', 'country' => 'Iran', ], ', 'name' => 'Pedro', 'country' => 'El Salvador', ], ] $additionalUpdateField = $sql = $builder -> setData ( $data ) -> updateFields ( $additionalUpdateField, true ) -> upsertBatch () /* MySQLi produces: INSERT INTO `db_user` (`country`, `email`, `name`) VALUES ('Iran',' ','Ahmadinejad'),('El Salvador',' ','Pedro') ON DUPLICATE KEY UPDATE `country` = VALUES(`country`), `email` = VALUES(`email`), `name` = VALUES(`name`), `updated_at` = CURRENT_TIMESTAMP */ You can either pass an array or an object to the Generates an insert string based on the data you supply, and runs the Inserting Data Insert $builder->insert() $builder->havingGroupEnd() Įnds the current group by adding a closing parenthesis to the HAVING clause of the query. Starts a new group by adding an opening parenthesis to the HAVING clause of the query, prefixing it with OR NOT. ![]() Starts a new group by adding an opening parenthesis to the HAVING clause of the query, prefixing it with NOT. Starts a new group by adding an opening parenthesis to the HAVING clause of the query, prefixing it with OR. Starts a new group by adding an opening parenthesis to the HAVING clause of the query. $builder->groupEnd() Įnds the current group by adding a closing parenthesis to the WHERE clause of the query. Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with OR NOT. Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with NOT. Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with OR. Starts a new group by adding an opening parenthesis to the WHERE clause of the query. Groups need to be balanced, make sure every groupStart() is matched by a groupEnd(). This sets the FROM portion of the query for youĪnd returns a new instance of the Query Builder class: The Query Builder is loaded through the table() method on theĭatabase connection. The same is true when using RawSql, which specifies a raw SQL statement. They are properly escaped or protected before passing it to the Query Builder. If $escape is set to false, no protection is provided by the Query Builder, Therefore, you should never feed in user input to them without proper validation.Īlso, many methods have the $escape parameter that can be set to disable escaping. ![]() However, it is implemented to work well in many use cases and ![]() It will also try to properly protect identifiers and identifiers in The Query Builder will escape all values by default. Identifiers such as field (or table) names Arguments passed to the Query Builder can be: It is not designed to prevent SQL injection no matter what data you pass. You can generate SQL statements quite safely with the Query Builder.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |