I'm trying to get all the users that has registered during a particular month using a query like this in a cakephp application:
$registered_users = $this->User->find('all', array(
'conditions' => array(
'MONTH(User.date)' => 10
)
));
The query above is supposed to return me all the users that has registered during the month of october for example, but for some reason, I get the following error:
1054: Unknown column 'User.date' in 'where clause'
Does anybody know why I'm getting this error please?
Thank you
Try this
$condition['MONTH(date) >'] = '10';
$registered_users = $this->User->find('all', array(
'conditions' => $condition,
));
Sorry I dont have enough points to add this as a comment.
Make sure in your database User.date column actually exists. Maybe you wanted to use User.created?
Related
I am try to fetch record using "not exists" and "exists" command.
its working fine with findAll .. but with pagination "MAX_JOIN_SIZE" issue rise
Code :
$this->User->unbindModelAll();
$this->Booking->unbindModelAll();
$this->User->bindModel(array('hasMany' => array('UserImage'), 'hasOne' =>array('Booking','RentalGoal', 'Verification', 'AbcCheck')), false);
$cond_user = array(
'User.id NOT' => $a,
'User.is_hide' =>1, 'User.status' =>1,
'RentalGoal.looking_for' =>1,'RentalGoal.room_type <>' => null, 'RentalGoal.step1_completed' =>1, 'Verification.rent_gaol' => 1,
'OR'=>array(
array('not exists ' . '(select id from dev_bookings ' . 'where dev_bookings.user_id = ' . 'User.id AND dev_bookings.move_in =1 AND dev_bookings.move_out = 0 AND dev_bookings.rent_term = "m")'),
array('exists ' . '(select id from dev_user_images ' . 'where dev_user_images.user_id = ' . 'User.id AND dev_user_images.status =1 AND dev_user_images.default = 1)'),
false),);
Working fine with >>
$new_arr = $this->User->find('all', array(
'conditions' => $cond_user,
'limit' =>400, 'recursive' => 2,
'order' => array('User.user_verification DESC'),
));
but not working with paginate
$this->paginate = array(
'conditions' => $cond_user,
'limit' =>100,
'recursive' =>2,
'order' => array('User.user_verification DESC'),
);
$new_arr = $this->paginate('User');
Error >
2014-07-04 14:38:51 Error: [PDOException] SQLSTATE[42000]: Syntax error or access violation: 1104 The SELECT would examine more than MAX_JOIN_SIZE rows; check your WHERE and use SET SQL_BIG_SELECTS=1 or SET MAX_JOIN_SIZE=# if the SELECT is okay
AppModel >
class AppModel extends Model {
function beforeFind($query)
{
$this->query('SET SQL_BIG_SELECTS=1');
}
}
what was the wrong with it? i also use ,false into bind model.
please help
thanks
MySQL determines whether or not a query is a 'big select' based on the value of 'max_join_size'. If the query is likely to have to examine more than this number of rows, it will consider it a 'big select'. Use 'show variables' to view the value of the max join size.
I believe that indexing and particular a good where clause will prevent this problem from occuring.
SQL_BIG_SELECTS is used to prevent users from accidentally executing excessively large queries. It is okay to set it to ON in mysql.cnf or using the command-line option at startup.
You can set SQL_BIG_SELECTS in my.cnf or at server startup. It can also be set on a session basis with 'SET SESSION SQL_BIG_SELECTS=1'.
Not that I can think of. I would just check your query to make sure that you really need to use it. Our servers have it turned on by default, and max_join_size is very large.
set max join size
SET GLOBAL max_join_size=1844674407370955148795;
http://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html
I have a very complex setup on my tables and achieving this via any of the find() methods is not an option for me, since I would need to fix relationships between my tables and I don't have the time right now, so I'm looking for a simple fix here.
All I want to achieve is run a query like this:
SELECT MAX( id ) as max FROM MyTable WHERE another_field_id = $another_field_id
Then, I need to assign that single id to a variable for later use.
The way I have it now it returns something like [{{max: 16}}], I'm aware I may be able to do some PHP on this result set to get the single value I need, but I was hoping there was already a way to do this on CakePHP.
Assuming you have a model for your table and your are using CakePHP 2.x, do:
$result = $this->MyTable->field('id', array('1=1'), 'id DESC');
This will return a single value.
see Model::field()
This example is directly from the CakePHP documentation. it seems you can use the find method of a model to get count
$total = $this->Article->find('count');
$pending = $this->Article->find('count', array(
'conditions' => array('Article.status' => 'pending')
));
$authors = $this->Article->User->find('count');
$publishedAuthors = $this->Article->find('count', array(
'fields' => 'DISTINCT Article.user_id',
'conditions' => array('Article.status !=' => 'pending')
));
I am trying to update multiple records in one field in my database. For some reason I keep getting SQL Error: 1054: Unknown column '520947b9' in 'field list'. 502947B9 is apart of my ID. Im not understanding why that value is being seen as a field list. Here is my code. That said, Im not sure Im updating these records correctly. If Im not please point it out to me. Thanks!!
public function findPolicyIds($coverageId = null) {
$policyid = $this->Policy->find('all', array(
'recursive' => -1,
'conditions' => array('Policy.coverage_id' => $coverageId),
'fields' => array('Policy.id')));
foreach($policyid as $id) {
$all[] = $id['Policy']['id'];
foreach ($all as $key) {
$this->Policy->Declination->updateAll(
array('Declination.policy_id' => $key),
array('Declination.coverage_id <=' => $coverageId)
);
}
}
}
Here are my errors
Query: UPDATE declinations AS Declination LEFT JOIN policies AS Policy ON (Declination.policy_id = Policy.id) SET Declination.policy_id = 520947b9-0210-4067-94ea-70f8ae78509d WHERE Declination.coverage_id <= '520947b9-1fa0-45db-992e-70f8ae78509d'
Query: UPDATE declinations AS Declination LEFT JOIN policies AS Policy ON (Declination.policy_id = Policy.id) SET Declination.policy_id = 520947b9-0694-4724-b353-70f8ae78509d WHERE Declination.coverage_id <= '520947b9-1fa0-45db-992e-70f8ae78509d'
By the looks of your query, updateAll is not recognizing $key as a string. Either cast it as such, or add the ' characters yourself. Example:
$this->Policy->Declination->updateAll(
array('Declination.policy_id' => "'".$key."'"),
array('Declination.coverage_id <=' => $coverageId)
);
That's the SQL error.
Now
"That said, Im not sure Im updating these records correctly."
... Well, what do you want to do? Reading your code, You are getting an array of Policy's ids and updating all Declinations with a coverage_id <= $coverageId, which doesn't make much sense, since that foreach is updating the policy_id for that same condition, so in the end you will perceive the last change: last policy_id of the foreach on every Declination with coverage_id equal or less than $coverage_id.... Doesn't make much sense to me, even not knowing what you need to do.
Based on the SQL and assuming you are using an ORM, it appears to me that policy_id is defined as an numeric field in your Declination model when it really needs to be a string. Coverage_id field is working correctly, so compare the two definitions.
I have 2 dates (create, update) that i want to merge in a new column, selecting the newest date... how can I do it?
Here is the array creation:
$this->Message= array(
'fields' => array('Message.id','Message.type','Message.createdate','Message.updatedate'),
'conditions' => $cond);
$messages = $this->Message->find('all', $conditionsMessage);
Now I need another field (lets call it NewDate) Message.NewDate that gets the newest date from Message.createdate and Message.updatedate, so i can call it after in a view using $messages[NewDate]
Help plz...
Thx!
UPDATE:
It's not hard to loop over the array with something like
foreach($messages as $k => $m){
if(strtotime($m['Message']['updatedate']) > strtotime($m['Message']['createdate'])){
$messages[$k]['Message']['NewDate'] = $m['Message']['updatedate'];
}else{
$messages[$k]['Message']['NewDate'] = $m['Message']['createdate'];
}
}
ORIGINAL ANSWER:
I would think that your Message.updatedate would always be the newest date, so you could just select that. But assuming that's not the case for some reason, you can create a virtual field in your model:
public $virtualFields = array(
'NewDate' => "IF(Message.updatedate > Message.createdate, Message.updatedate, Message.createdate)"
);
This uses the MySQL IF() function. If you're not using MySQL you'd have to figure out how to do something similar with your database.
http://dev.mysql.com/doc/refman/5.0/en/control-flow-functions.html#function_if
Hello I have been trying to understand how to get data from model by the name of field. I am using cakePHP, and I need to retreive a column's data from a table. The syntax is
> "select name from permissions"
So I tried to find out on book.cakephp.org, so I got the field function, but that only gives me the first value, while I have more than one values for this.
I tried do a
$this->Model->find(array('fields'=>'Model.fieldName'));
but I understood that the syntax itself is flawed.
Can somebody let me know what is the method to query based on column name.
$this->Model->find(array('fields'=>'Model.fieldName'))
You forgot the array function. Also:
$this->Model->find(array('fields'=>array('Model.fieldName')))
will work.
findAllBy will find all records based on the field name.
$this->Model->findAllBy<fieldName>(string $value, array $fields, array $order, int $limit, int $page, int $recursive);
For eaxample:
$this->Permission->findAllByName('Some Name');
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#findallby
Found it... hope it will help someone.
$workshop_lists = ClassRegistry::init('Workshop')->find('all',array(
'fields'=>array('user_id', 'title')
),
array(
'conditions' => array('user_id' => $this->Auth->user('id')),
'group' => 'Workshop.user_id',
'order' => 'posted DESC',
));
There is no way you can query out based on column name using one of the cake methods. You have to use the query method.
Syntax: $this->Model->('Select columnname from table');
$this->Model->find('all',array('fields'=>array('Model.fieldName')))
it works for me everytime.
If I understood well and you want not only 1 value but the whole values in the column 'name' from the table 'permissions'. In that case you could use:
$this->Model->find('list',$params);
(see explanation for 'find' here)
for the '$params' part you would use:
$params=array('fields'=>array('name'));
or putting all in a single line:
$arrayOfNames= $this->Model->find('list',array('fields'=>array('name')));
This will give you an array '$arrayOfNames' wich key is the 'id' (primary key) in 'permissions' table and wich value is the corresponding name in the field 'name' from the same table. This is the array would be something like:
'id'=>'name'
[23]=>'name1'
[28]=>'name2'
[29]=>'name3'
............
very much like I think you want. Hope it helps.
$this->Model->find('list', ['valueField' => 'fieldName']);