Can i give conditions in contain in cakephp 1.3 - cakephp

i m facing some problem in find function. im fetching value fro find function.i have model.contian another model .
Can i give conditions in contain in cakephp
is it possiblehi

$this->Post->find('all', array('contain' => array(
'Comment' => array(
'conditions' => array('Comment.author =' => "Daniel"),
'order' => 'Comment.created DESC'
)
)));
You should always try the documentation first :).

isnt conditions in the format $this->ModelName->find('all',array('conditions'=>array('Table.name'=>1))); working for you?

Yes you can give conditions in contain

Related

Cakephp Containable not working at all

I have been banging my head on the wall over this. I have a model Sku that belongs to model Purchase. My AppModel has $actAs=array('Containable') and $recursive=-1
Inside SkuController, when I do $this->Sku->find('all', array('contain' => 'Purchase')); I don't get Purchase. I have searched many old questions here and elsewhere on Internet but just can't seem to resolve this. To check if Containable behavior is being loaded, I edited ContainableBehavior.php in lib\Cake\Model\Behavior to make it an invalid php file but that didn't produce any errors. What the heck is wrong!!
Here's the SQL from debug:
SELECT Sku.id, Sku.purchase_id, Sku.item_id, Sku.upc,
Sku.quantity_avail, Sku.per_unit_price_amt,
Sku.do_not_delete, Sku.created, Sku.modified,
(concat('SK',lpad(Sku.id,8,'0'))) AS Sku__idFormatted FROM
sellble.skus AS Sku WHERE 1 = 1 ORDER BY Sku.id desc
CakePHP ver: 2.4.4
Not sure if this is different across versions but I have always specified the contain within an array and that works fine for me.
$this->Sku->find('all', array('contain' => array('Purchase')));
Or for mapping only the fields or conditions you want:
$this->Sku->find('all',
array('contain' => array(
'Purchase' => array(
'fields' => Purchase.name
'conditions' => array(
Purchase.name = 'somename'
)
)
)
)
);

CakePHP 2.4.4 How can I structure this find() with Containable?

Tables
User(id)
Profile(id, *user_id*, type)
Attribute(id, *attribute_name_id*, value)
AttributeName(id, name)
ProfileAttribute(id, *profile_id*, *attribute_id*)
Relationships
The relationships are set up correctly (and go both ways, hasMany/belongsTo).
User hasMany Profile
Profile hasMany ProfileAttribute
Attribute hasMany ProfileAttribute
(could be written Profile hasMany Attribute through ProfileAttribute)
AttributeName hasMany Attribute
Goal
For a specified User id, with a find() in the User model, I only want the following fields, laid out as such:
$results[Profile.type][AttributeName.name][Attribute.value]
Is it even possible to retrieve results arranged like this? I've been playing around with Find and Containable for hours, but, first time trying to do anything complicated like this with Cake, I can't get the hang of it.
Thanks!
EDIT
I'm getting these results now, all that I need, but nowhere near the desired format above -can it be done as part of the find, or does it need to be sorted after?
Yep, it's possible. You just have to specify fields on containable:
$this->User->find('all', array(
'conditions' => array('User.id' => $id),
'fields' => array('id'),
'contain' => array(
'Profile' => array(
'fields' => array('id','type'),
'ProfileAttribute' => array(
'fields' => array('id'),
'AttributeName' => array(
'fields' => array('id','name'),
'Attribute' => array(
'fields' => array('id','value')
)
)
)
)
)
);
Be wary that when you use contain and fields options, you have to specify the id so it can make the association (check the docs)
EDIT: I don't know if you can group contained data as the docs didn't say anything about that, but probably you can, as they accept some parameters as in the main query. You can try it, adding group to any contained data that you want to group

Cakephp change the order of virtualFieldId in find/paginate

I have a model in which I have defined a virtualfield, virtualfield01. And when I call a find function like this.
$this->myModel->find('all', array(
'fields' => array(
'field01',
'virtualfield01',
'field02',
'field03')));
the result always gives me.
myModel=>array(
'field01' => 'value01'
'field02' => 'value02'
'field03' => 'value03'
'virtualfield01' => 'virtualvalue01')
the virtualfield is always output as the last field of the result.
How can I make the order exactly the same to that I make in the find function???
Why wouldn't you just do something like the following in your view, just as #mark mentioned?
$this->Html->tableCells(array(
$var['Model']['field01'],
$var['Model']['virtualfield01'],
$var['Model']['field02'],
$var['Model']['field03'],
));
There's no 're-arrangement action', you're just telling the data how to display, which is why it is a 'view'.
Simple is that as mark Mentioned. Order doesn't matter.

Containable behavior issue with non-standard keys?

I'm working with a legacy database whose keys follow a convention, but not the cake convention, unfortunately, and I've bumped into something odd with the containable behavior -- it's pulling the wrong data. Here's my setup:
TechnologyIncentive belongsTo...
array(
'Incentive' => array(
'className' => 'Incentive',
'type' => 'inner',
'foreignKey' => false, # actually 'incentive_id', but we need to fool Cake
'conditions' => array( 'TechnologyIncentive.incentive_id = Incentive.incentive_id' ),
),
'Technology' => array(
'className' => 'Technology',
'type' => 'inner',
'foreignKey' => false, # actually 'incentive_tech_id', but we need to fool Cake
'conditions' => array( 'TechnologyIncentive.incentive_tech_id = Technology.incentive_tech_id' )
),
);
You can see that I've had to fool Cake into working with my non-standard keys by setting the foreignKey to false and defining the link in my where clause. So far so good.
Now, when I try to run a query from the TechnologyIncentive model:
$this->find( 'all', array(
'contain' => array( 'Incentive', 'Technology' ),
'fields' => array( 'Incentive.name', 'Technology.name', 'TechnologyIncentive.amount' ),
'conditions' => array( /** conditions... */ )
);
Everything works great. Stuff is nicely contained and I get exactly what I'd expect. Then I need to include a TechnologyGroup, which hasMany Technology and things breakdown for some reason.
Now my contain option looks like this:
'contain' => array( 'Incentive', 'Technology' => array( 'TechnologyGroup' ) )
And what I get back is an Incentive record contains an incentive record. That's not entirely surprising since I'm specifying a few fields in one place (the main fields option) and implicitly all fields in the contain option, but what's really weird to me is that they're different incentives. The "contained" incentive is just wrong.
Inspecting the SQL, it looks like the query was run with no effective where clause at all, so everything is being pulled and then artificially limited to a single record. Note the difference between $result['Incentive']['incentive_id'] and $result['Incentive']['Incentive']['incentive_id'].
Array
(
[Incentive] => Array
(
[incentive_id] => MD046
[name] => Incentive name
[category] =>
[Incentive] => Array
(
[incentive_id] => AK004
[code] => AK04F
[version] => 2
[category] => Incentive Category
)
)
)
Has anyone ever bumped into this? It's not a problem until I want to retrieve an extended record (TechnologyGroup). Any thoughts would be much appreciated.
It looks like this was directly attributed to the non-standard keys. The Containable behavior, more so, it seems, that other elements of Cake really wants the convention followed. Moreover, some of the key associations included a double underscore (__) in the field name and that caused other problems.
Kids, don't try this at home. Follow the conventions, even if it means manipulating a legacy database.

Cakephp find problem

I want to query as below in cakePHP.so, How can I do query using 'find' function of cakePHP .
Help me.
SELECT DISTINCT(Property.City),'city' as Fieldname FROM `properties` as Property WHERE Property.City LIKE 'las%'
Try:
$this->Property->find(
'all',
array(
'fields' => array( 'DISTINCT ( Property.city )' ),
'conditions' => array( 'Property.city LIKE' => 'las%' )
)
I threw this up from memory, but it should be close. I'm also sure that DISTINCT queries are at least mentioned in the documentation. Probably in the "complex finds" section.
Hope that helps some.

Resources