Codeigniter run query before a update - database

I have to run a query after every update and I want to know if there is a way to automate a $this->db->query() before every $this->db->update()
I'm using it for log.

you can write your own function in the file core/MY_Model.php to do that:
function queryThenUpdate($query,$update)
{
$query = $this->db->query($query);
//use as you need $query
$this->db->update($update['table'],$update['data']);
}
where:
$query is your actual query: SELECT * FROM ...
$update is an array of two elements $update['table'] is the table to update and $update['data'] is the updating data as specified on codeigniter active record's documentation
then make every model extend MY_Model
class Your_Model extend MY_Model
and every time you need to update something:
$this->Your_Model->queryThenUpdate($query,$update)

I believe you want to use codeigniters 'hook'

Related

CakePHP 3.x Specify a Different Count Query for Pagination

I am trying to customize the pagination query for CakePHP I am using a Model based around a view. The view has some complex calculations in it and when the pagination runs it takes about 3 seconds to pull the 20 rows or so. Then it is running the same view wrapped in a count for the count used in pagination. I can run a simple table count on another table which would cut the load times in half but I am unable to find a simple way to customize a seperate query to run for the counts. Is this even possible?
I know another user asked this question but it was never answered.
Cakephp 3.x Custom Query Pagination
I have tried the way that is used to work in 2.x using a custom paginateCount function in both the entity and the table and neither works I dug through the API a bit but couldn't find anything related to the count specifically.
One option would be to use the query builder's counter() method to provide a callback that returns the number of records based on your custom query.
$query->counter(function (\Cake\ORM\Query $query) {
// assuming $this uses \Cake\ORM\Locator\LocatorAwareTrait
$OtherTable = $this->getTableLocator()->get('Other');
$otherQuery = $OtherTable->find();
// ...
return $otherQuery->count();
});
The $query argument will be a clone of the query itself. You can either modify that one, or construct a completely new query.
See also
Cookbook > Database Access & ORM > Query Builder > Returning the Total Count of Records
API > \Cake\ORM\Query::counter()

How to use function find('list') get data from other table beside its model?

I am new of Cakephp I stuck by using function find('list') to get data from other table inside User model. Example i have User Model want get List of group andgroup has table "groups" but i don't want create php files for group controller and group model.
<?php
//User model
class User extends AppModel {
public function getGroupList() {
/* Here i want return list all group by use function find('list')
* but group has table name "groups" and i don't want use sql query string.
* Note that group i don't create files php in controller and model.
*/
}
}
?>
Why don't you want to create a groups controller? Doing things cakes way will often make your life easier.
There will be an extra sql query (or join at least) involved in either case.

In CakePHP, is there a better way to select the users name?

I currently have a users table and a posts table. My posts table has a field for user_id, however, I am not sure how I would grab the users name field which is in the users table. I was thinking of using the models afterFind() method and then using SQL to select the data, but there has to be a better way than this. Also, on my view action, I am using the read() function to grab a single post. Would the models afterFind() kick in after it runs read()? If not, is there an equivalent such as afterRead()?
Just make an association of Post belongsTo User, and every regular find/read operation that has a sufficiently high recursive value will automatically fetch the user with each post.
$post = $this->Post->find(...);
echo $post['User']['name'];
you have to go like below :
$this->Cake->findById(7); Cake.id = 7
here , you can you use your user_id instead of 7 like..
Find BY CAKE PHP
$this->Post->bindModel(array('belongsTo'=>'User'));
$post = $this->Post->findById($post_id);
$userName = $post['User']['name'];
Here are errors, I've typed it in eclipse (:

CAKEPHP Retrieve post from a Db table skipping the first 2 results

I would like to retrieve data from a specific table in the Db, but skip the first 2 results. Supposed I have the following:
An Articles controller with an Index action
I would like to use $articles = $this->Articles->find('all' ...
What code should I use to place those results into the $articles variable.
Thanks in advance.
You need to change the offset / limit of your query.
http://book.cakephp.org/view/1018/find
$articles = $this->Articles->find('all', array('offset' => 2));

Update the value of a field in database by 1 using codeigniter

I want to implement a SQL statement using codeigniter active record.
UPDATE tags SET usage = usage+1 WHERE tag="java";
How can I implement this using Codeigniter active records?
From the documentation:
set() will also accept an optional third parameter ($escape), that will prevent data from being escaped if set to FALSE.
So this should work to pass the increment statement directly to the database:
$this->db->set('usage', 'usage+1', FALSE);
$this->db->where('tag', 'java');
$this->db->update('tags');
Because it's not escaped, if you're using a variable instead of a fixed number it should be verified as numeric beforehand.
You can also use something like this
$data = array('usage' => 'usage+1', *other columns*);
$this->db->where('tag', 'java');
$this->db->update('tags', $data);
UPDATE: $data was not being passed on to update
I find its sometimes simpler to just write the SQL rather than having Active Record build it for me.
$sql = 'update tags set usage=usage+1 where tag=?';
$this->db->query($sql, array($tag));

Resources