CakePHP adding array elements to an array - arrays

I think I have my dumber-than-usual head on today.
I am trying to parse text from a textbox in a form to generate an array that I can pass to the find method in cakePHP.
I have used php regular expressions to generate an array of the terms entered (including "phrases included in speech marks" as single item)
This may look like this:
array(
(int) 0 => 'test',
(int) 1 => 'this out',
(int) 2 => 'now')
In the cakePHP cookbook it says that I have to get an array that looks like this (using above example):
array(
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'),
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'),
array('ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'))
Try as I might, I end up with arrays that do NOT look like an array of arrays.
Typically I am ending up with this horrible looking thing:
array(
(int) 0 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%'
),
(int) 1 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%'
),
(int) 2 => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
))
The code for the above is:
$i = 0;
foreach($matches[1] as $match) :
$searcharray['ProjectDocumentSectionVariable.variable_description LIKE'] = "%" . $match . "%";
array_push($array_full,$searcharray);
$i++;
endforeach;
THis must be a common requirement, so I am hoping one of you guys could put me on the straight an narrow...
Thanks

If you're trying to build a search query (not clear what your goal is), your conditions should all be in the same array within an OR or AND - something like this:
$searchResults = $this->find('all', array(
'conditions' => array(
'OR' => array(
'ProjectDocumentSectionVariable.variable_description LIKE' => '%test%',
'ProjectDocumentSectionVariable.variable_description LIKE' => '%this out%',
'ProjectDocumentSectionVariable.variable_description LIKE' => '%now%'
)
)
));
If you want to build it out in a foreach() loop, you can do it something like this:
$conditions = array('OR' => array());
$matches = array('test', 'this out', 'now');
foreach($matches as $match) {
array_push($conditions['OR'], array('variable_description LIKE' => '%' . %match . '%'));
}
$searchResults = $this->find('all', array(
'conditions' => $conditions
));

its as simple as this:
$array= array();
foreach ( $colors as $c):
$uniques[] = $c;
endforeach;
not having the [] will treat it as a regular variable. This will append it to the array

Related

How to add multiple conditions for a single column?

I have many sentences that I need filter for a same column:
'conditions' => array('Zona.nombre LIKE' => $buscar,
'Zona.nombre LIKE' => 'CUPONATIC%',
'Zona.nombre LIKE' => 'GROUPON%'
),
your question is not very clear but I suppose the problem is that you use multiple time the same array key
You don't even mention the cakephp version but it seems cake2
If I remember well the workaround for cake2 is putting every condition in a different array
'conditions' => array(
array('Zona.nombre LIKE' => $buscar),
array('Zona.nombre LIKE' => 'CUPONATIC%'),
array('Zona.nombre LIKE' => 'GROUPON%')
),
edit: of course this way you'll have the 3 conditions joined in AND.
It seems more logical to put them in OR so
'conditions' => array(
'OR' => array(
array('Zona.nombre LIKE' => $buscar),
array('Zona.nombre LIKE' => 'CUPONATIC%'),
array('Zona.nombre LIKE' => 'GROUPON%')
)
),

Search with concat fields in cakephp 3

I need to make a search query using $this->Paginate in CakePHP 3. Following is the code I am using
$searchCondition = array(
'OR' => array(
'Quotes.quotenum LIKE' => "%" . $this->request->data['Quote']['keyword'] . "%",
'Branches.name LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
'Contacts.fname LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
'Contacts.lname LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
'CONCAT(Contacts.fname, Contacts.lname) LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%',
'Quotes.description LIKE' => '%' . $this->request->data['Quote']['keyword'] . '%'
)
);
$cond = array(
'conditions' => array_merge($searchConditions, $searchCondition, $limo),
'order'= > array('Quotes.quotenum desc'),
'contain' => array('Branches','Contacts')
);
$this->set('articleList', $this->paginate($this->Quotes));
As you can see I merge the condition arrays with each other and then send them to paginate. This worked fine in CakePHP 2.7. However now I get the error
Column not found: 1054 Unknown column 'contacts.lname' in 'where clause'.
The lname column definitely exits in the database table. Is there something I am doing wrong. If so, could someone tell me the right way to do concat search as I have been trying to do this for quite some time.
Yu have to use query expression but this can't be done in a pagination array.
So Following ndn suggestion here's how I would do
create a custom finder. In your QuotesTable file
public function findByKeyword(Query $query, array $options)
{
$keyword = $options['keyword'];
$query->where(
function ($exp, $q) use($keyword){
$conc = $q->func()->concat([
'Contacts.fname' => 'literal', รน
'Contacts.lname' => 'literal']);
return $exp
->or_([
'Quotes.quotenum LIKE' => "%$keyword%",
'Branches.name LIKE' => "%$keyword%",
'Contacts.fname LIKE' => "%$keyword%",
'Contacts.lname LIKE' => "%$keyword%",
'Quotes.description LIKE' => "%$keyword%"
])
->like($conc, "%$keyword%");
}
);
return $query;
}
Then in your controller
$this->paginate = [
'finder' => [
'byKeyword' => [
'keyword' => $this->request->data['Quote']['keyword']
]],
'conditions' => $limo, // this will merge your $limo conditions
// with the ones you set in the custom finder
'order'= > ['Quotes.quotenum desc'],
'contain' => ['Branches','Contacts']
];
$this->set('articleList', $this->paginate($this->Quotes));

How to apply && between 3 $conditions[] ? in cakephp

In Cakephp how can i add apply 'AND' between these lines , $conditions[]= codition1 && codition2 && condition3;
$conditions[]= array('Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%");
$conditions[]= array('Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%");
$conditions[]= array('Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%");
You can simplify your query by using:
$conditions = array(
'Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%",
'Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%",
'Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%"
);
You don't actually need the arrays around each field when the conditions are on different fields. Plus CakePHP will automatically use AND between your conditions.
$conditions = array(
'AND' => array(
'Flight.from LIKE' => "%".$this->request->data['Flight']['from']."%",
'Flight.to LIKE' => "%".$this->request->data['Flight']['to']."%",
'Flight.date LIKE' => "%".$this->request->data['Flight']['date']."%"
)
);
Official docs and examples here.

CakePHP - How to use SQL NOW() in find conditions

I am having trouble getting any find operations to work using the SQL function NOW() in my conditions.
I am effectively trying to build a find query that says:
Desired SQL:
WHERE (NOW() BETWEEN Promotion.start AND Promotion.end) AND Promotion.active = 1
I have tried many combinations but no matter what I do when using NOW() in the condition, it doesn't work because the query Cake builds puts ' quotation marks around the model fields so they are interpreted by MySQL as a string.
$this->find('all', array(
'conditions' => array(
'(NOW() BETWEEN ? AND ?)' => array('Promotion.start', 'Promotion.end'),
'Promotion.active' => 1
)
));
CakePHP created SQL:
Notice the single quotes around the model fields in the BETWEEN(), so they are treated as strings.
WHERE (NOW() BETWEEN 'Promotion.start' AND 'Promotion.end') AND `Promotion`.`active` = '1'
This doesn't work either.
$this->find('all', array(
'conditions' => array(
'NOW() >=' => 'Promotion.start',
'NOW() <=' => 'Promotion.end',
'Promotion.active' => 1
)
));
I know why these solutions don't work. It's because the model fields are only treated as such if they are the array key in the conditions, not the array value.
I know I can get this to work if I just put the whole BETWEEN() condition as a string:
$this->find('all', array(
'conditions' => array(
'NOW() BETWEEN Promotion.start AND Promotion.end',
'Promotion.active' => 1
)
));
Another example of the same problem is, which is simpler to understand:
Desired SQL:
WHERE Promotion.start > NOW() AND Promotion.active = 1
So I try this:
$this->find('all', array(
'conditions' => array(
'Promotion.start >' => 'NOW()',
'Promotion.active' => 1
)
));
And again it doesn't work because Cake puts ' quotations around the NOW() part.
CakePHP created SQL:
WHERE `Promotion`.`start` > 'NOW()' AND `Promotion`.`active` = '1''
$this->find('all', array(
'conditions' => array(
'NOW() BETWEEN Promotion.start AND Promotion.end',
'Promotion.active' => 1
)
));
Better to not use NOW() as its a function and functions don't use indexes. A better solution would be:
$this->find('all', array(
'conditions' => array(
"'" . date('Y-m-d') . "' BETWEEN Promotion.start AND Promotion.end",
'Promotion.active' => 1
)
));

CakePHP find condition for a query between two dates

I have a start and an end date in my database and a $date variable from a form field. I am now trying to query all the rows where $date is either = start/end date in the db, or ANY date between those two.
It's kind of the opposite of what is described in the docs of how daysAsSql works. I can't figure out how to get it to work. The following line does not work as a find condition in the controller:
'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
Any help is greatly appreciated. This is driving me crazy.
Here is the complete Query and corresponding SQL:
$conditions = array(
'conditions' => array(
'and' => array(
'? BETWEEN ? AND ?' => array($date, 'Item.date_start', 'Item.date_end'),
'Item.title LIKE' => "%$title%",
'Item.status_id =' => '1'
)));
$this->set('items', $this->Item->find('all', $conditions));
WHERE (('2012-10-06' BETWEEN 'Item.date_start' AND 'Item.date_end') AND (`Item`.`title` LIKE '%%') AND (`Item`.`status_id` = 1))
$conditions = array(
'conditions' => array(
'and' => array(
array('Item.date_start <= ' => $date,
'Item.date_end >= ' => $date
),
'Item.title LIKE' => "%$title%",
'Item.status_id =' => '1'
)));
Try the above code and ask if it not worked for you.
Edit:
As per #Aryan request, if we have to find users registered between 1 month:
$start_date = '2013-05-26'; //should be in YYYY-MM-DD format
$this->User->find('all', array('conditions' => array('User.reg_date BETWEEN '.$start_date.' AND DATE_ADD('.$start_date.', INTERVAL 30 DAY)')));
Here is CakePHP BETWEEN query example.
I'm defining my arrays as variables, and then using those variables in my CakePHP find function call:
// just return these two fields
$fields = array('uri', 'page_views');
// use this "between" range
$conditions = array('Event.date BETWEEN ? and ?' => array($start_date, $end_date));
// run the "select between" query
$results = $this->Event->find('all',
array('fields'=>$fields,
'conditions'=>$conditions));
Ref from
General Example For CakePHP 2.x Query
$_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
$result_array = $this->TABLENAME->find("all", array(
'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
"conditions" => $_condition,
"group" => array("TABLENAME.id"), //fields to GROUP BY
'joins' => array(
array(
'alias' => 'T2',
'table' => 'TABLENAME2',
'type' => 'LEFT',
'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
),
array(
'alias' => 'T3',
'table' => 'TABLENAME3',
'type' => 'LEFT',
'conditions' => array(
'IF(
TABLENAME.t3_id > 0,
T2.f_id = T3.f_id,
TABLENAME.ff_id = T2.ff_id
)'
)
),
),
'recursive' => 0
)
);
This is more efficient and understandable IN BETWEEN query in cakephp 2.x
$testing_log_device_site_name = $testingLogData['TestingLogDevice']['Siteid'];
$conditions = array('TestingLogDevice.dateee BETWEEN ? and ?' => array($start_date, $end_date));
$results = $this->TestingLogDevice->find('all',
array(
'fields'=>array('dateee','timeee','Siteid'),
'conditions'=>array($conditions, 'TestingLogDevice.Siteid'=>$testing_log_device_site_name)
)
);
pr($results);
$data=$this->post->find('all')->where([ 'id'=>$id,
'price between'=>$price1,'and'=>$price2])->toArray();
This query works as following:
select * from post where id=$id and price between $price1 and $price2;
" -- 'price between'=>$price1 --" become "price between $price1"
Use this
$today = new DateTime( date('Y-m-d'));
$fiveYearsBack = $today->sub(new DateInterval('P5Y'));

Resources