Conditions with And Or statements - sql

I got this figured out. Here is the solution:
'conditions'=>array(
'OR' => array(
array('EavAttribute.attribute_code'=>'lastname'),
array('EavAttribute.attribute_code'=>'firstname')
),
'AND' => array(
array('UserEntityVarchar.entity_id'=>$id)
)
)
I am trying to convert this query into a cakephp query and I am having a bit of trouble with the conditions. No matter how I format the conditions, I always end up with the second query below.
Any help with this is greatly appreciated.
This is the query I am trying to replicate:
SELECT
`UserEntityVarchar`.`value_id`,
`UserEntityVarchar`.`attribute_id`,
`UserEntityVarchar`.`entity_id`,
`UserEntityVarchar`.`value`,
`EavAttribute`.`attribute_code`
FROM
`user_entity_varchars` AS `UserEntityVarchar`
LEFT JOIN `eav_attributes` AS `EavAttribute` ON(
`UserEntityVarchar`.`attribute_id` = `EavAttribute`.`attribute_id`)
WHERE
(UserEntityVarchar.entity_id = 1 AND
EavAttribute.attribute_code = 'firstname') OR
(UserEntityVarchar.entity_id = 1 AND
EavAttribute.attribute_code = 'lastname')
This is the query I keep getting no matter how I format my condition:
SELECT
`UserEntityVarchar`.`value_id`,
`UserEntityVarchar`.`attribute_id`,
`UserEntityVarchar`.`entity_id`,
`UserEntityVarchar`.`value`,
`EavAttribute`.`attribute_code`
FROM
`user_entity_varchars` AS `UserEntityVarchar`
LEFT JOIN `eav_attributes` AS `EavAttribute` ON(
`UserEntityVarchar`.`attribute_id` = `EavAttribute`.`attribute_id`)
WHERE
((`UserEntityVarchar`.`entity_id` = 1)
AND
(`EavAttribute`.`attribute_code` = 'firstname'))
AND
((`UserEntityVarchar`.`entity_id` = 1)
AND
(`EavAttribute`.`attribute_code` = 'lastname'))
This is the condition that I am using:
'conditions'=>array(
array(
array('UserEntityVarchar.entity_id'=>$id),
array('AND ' => array('EavAttribute.attribute_code'=>'firstname'))
),
array('OR' =>
array('UserEntityVarchar.entity_id'=>$id),
array('AND ' => array('EavAttribute.attribute_code'=>'firstname'))
)
)

You should be able to do it like this:
$this->Model->find('all', array('conditions' => array('OR' => array(
array('UserEntityVarchar.entity_id' => 1,
'EavAttribute.attribute_code' => 'firstname'),
array('UserEntityVarchar.entity_id' => 1,
'EavAttribute.attribute_code' => 'lastname')))));

Related

Filter on my module for Prestashop 1.6

I'm trying to create a custom list for my module on Prestashop 1.6 and I need to get data from three different tables. My problem is the $this->_filter variable, how can I do that?
I need to do this:
$query = '
SELECT s.*, pl.*
FROM '._DB_PREFIX_.'`scd_gift` s
INNER JOIN '._DB_PREFIX_.'product_lang pl
ON s.id_product = pl.id_product
WHERE s.`id_gift_type` = '.(int)$id_gift_type.' and id_lang='.$id_lang;
Here is my function:
public function getCustomListHostessGifts() {
$this->table = 'scd_gift';
$this->list_id = 'hostess_gift';
$this->lang = true;
$this->identifier = 'id_scd_gift';
$this->_orderBy = 'id_product';
$this->_orderWay = 'DESC';
$this->addRowAction('delete');
$this->fields_list = (array(
'id_product' => array('title' => $this->l('ID'), 'class' => 'fixed-width-xs',
'align' => 'center'),
'name' => array('title' => $this->l('Name'), 'filter_key' => 'b!name'),
));
$this->clearFilters();
$hostessType = MlmGiftsModule::getGiftTypeIdByGiftTypeName('_HOSTESSGIFT_');
$this->_join = Shop::addSqlAssociation('scd_gift', 'a');
$this->_filter = '
INNER JOIN '._DB_PREFIX_.'product_lang pl
ON s.id_product = pl.id_product
AND a.`id_gift_type` ='.$hostessType ;
$this->toolbar_title = $this->l('Hostess gifts:');
return $this->renderList();
}
_filter is use to set filter parameters passed in backoffice list search fields.
Any JOIN sentence must be set in _join var:
$this->_join = Shop::addSqlAssociation('scd_gift', 'a');
. ' INNER JOIN '._DB_PREFIX_.'product_lang pl
ON s.id_product = pl.id_product
AND a.`id_gift_type` ='.$hostessType ;
Good luck.
PixelWeb's answer is correct, but of course, there is no semicolon after
$this->_join = Shop::addSqlAssociation('scd_gift', 'a')
Additional. You may skip this line anyway, because Prestashop adds the default SQL association "a" for the table in the controller itsself.

CActiveDataProvider Querying on join

I have a simple SQL query, which I need to convert to use in Yii 1.1.
SELECT *
FROM User
INNER JOIN Role ON Role.UserId = User.Id
WHERE Role.Name = 'admin'
How is this written into the CActiveDataProvider?
I have came up with an answer. Hopefully it helps someone in the future.
$dataProvider = new ActiveDataProvider('User', array
(
'criteria' => array
(
'with' =>'roles',
'join' => 'INNER JOIN Role r ON r.UserId = User.Id',
'condition' => 'r.Name=:term',
'params' => array(':term'=>'admin')
)
));

Cakephp: find in a controller using "like" inside an array of possible values

The main idea of this post is that I need to combine search inside an array via "like" without doing a fetch.
In the following code I extract the zones (part of postal code) depending on session ID:
$sess_id = $this->Session->read('Auth.User');
$this->loadModel('Zone', 2);
$MyZones = $this->Zone->find('list', array('recursive' => -1, 'conditions' => array('Zone.user_id' => $sess_id['id']), 'fields' => array('Zone.zone')));
$this->set('MyZones', $MyZones);
print_r($MyZones);
//output : $MyZone = Array ( [1] => AB [2] => AL [3] => B1 )
Now that I have my array I want to look for persons who live in this zone (example his postalcode can be AB526PQ so he lives in the administrator zone)
$this->loadModel('Person', 2);
$num_persons = $this->Person->find('count', array('recursive' => -1, 'conditions' => array('Person.pc LIKE' => '%' . $MyZones)));
$this->set('num_persons', $num_persons);
I got the following error :
Notice (8): Array to string conversion [APP\Controller\ZonesController.php]
This is the generated sql query:
SELECT COUNT(*) AS `count` FROM `tn`.`personnes` AS `Personne` WHERE `Personne`.`pc` LIKE '%Array'
You will have to break your condition from
$conditions = array('Person.pc LIKE' => '%' . $MyZones);
to
foreach($MyZones as $MyZone) {
$conditions[] = array('Person.pc LIKE' => '%' . $MyZone);
}

Rails Arel equivalent of this complex sql query

Here is the original logic
(scrape_datas = ScrapeData.find(
:all, :conditions =>
"artist_status = 'NOT_FOUND'
AND blacklisted = 1
AND extracted = 0
and not EXISTS(
SELECT * FROM artist_name_suggestions where original = artist_name
)
I've been able to split up the first part better
scrape_datas = ScrapeData.where(
:artist_status => 'NOT_FOUND',
:blacklisted => 1,
:extracted => 0
)
Although having issues getting the "and not EXISTS" query into the mix
and not EXISTS(
SELECT * FROM artist_name_suggestions where original = artist_name
)
Thanks!
Firstly you can extract simple scopes:
scope :not_found, where(:artist_status => 'NOT_FOUND')
scope :blacklisted, where(:blacklisted => 1)
scope :extracted, where(:extracted => 0)
Then add a query method (assume artist_name is a column of scrape_datas):
def self.no_suggestions
scrape_datas = ScrapeData.arel_table
suggestions = ArtistNameSuggestion.arel_table
where(ArtistNameSuggestion.where(
suggestions[:original].eq(scrape_datas[:artist_name])
).exists.not)
end
Now you can do something like this:
ScrapeData.not_found.blacklisted.extracted.no_suggestions

converting sql query into cakephp format

Hi
i need the following sql query into cakephp find() format. the query it self is working fine but i need to change it.
$this->Part->query(
"SELECT `parts`.`id`,`parts`.`part_name`
FROM `parts`
LEFT JOIN (
SELECT `op` . *
FROM `order_parts` AS `op`
WHERE `op`.`order_id` =".$this->Session->read('orderid')."
) AS `vT`
ON ( `parts`.`id` = `vT`.`part_id` )
WHERE `vT`.`part_id` IS NULL"
);
thanks
If your relationship are Order HABTM Part and you have a table orders_parts with columns: id, order_id,part_id you should be able to do something like this:
First, get the ids of the parts which are in the order:
//each Part has one OrdersPart per order
$this->Part->bindModel(array('hasOne' => array('OrdersParts')));
$parts = $this->Part->find('list', array(
'fields' => array('Part.name'),
'conditions' => array(
'OrdersParts.order_id' => $this->Session->read('orderid'),
),
'recursive' => 2
));
Now get the parts which are not in the order:
$this->Part->find('all', array(
'conditions' => array(
"NOT" => array('Part.id' => array_keys($parts))
),
));