Still tables with hasMany relationships not working.
Advert model:
var $hasMany = array(
'AdImage' => array(
'className' => 'AdImage',
'foreignKey' => 'advert_id',
'conditions' => '',
'order' => '',
'limit' => '5',
'dependent'=> true
),
'AdvertComfort' => array(
'className' => 'AdvertComfort',
'foreignKey' => 'advert_id',
'conditions' => '',
'order' => '',
'limit' => '5',
'dependent'=> true
));
AdImage (same AdvertComfort) model:
var $belongsTo = array(
'Advert' => array('className' => 'Advert',
'foreignKey' => 'advert_id',
'conditions' => '',
'fields' => '',
'order' => ''
));
find function:
$filter = array(
'conditions' => array(
'AdImage.url' => 'test.jpg'
),
'recursive' => 2
);
$rez = $this->Advert->find('all', $filter);
SQL:
SELECT `Advert`.`id`, `Advert`.`user_id`, `Advert`.`ad_category_id`,
`Advert`.`code`, `Advert`.`valid`, `Advert`.`bad`, `Advert`.`created`,
`AdCategory`.`id`, `AdCategory`.`alias`, `AdCategory`.`name`,
`AdsAuto`.`id`, `AdsAuto`.`advert_id`, `AdsAuto`.`ad_manufacturer_id`,
`AdsAuto`.`ad_model_id`, `AdsAuto`.`ad_type_id`, `AdsAuto`.`year`,
`AdsAuto`.`ad_fueltype_id`, `AdsAuto`.`ad_door_id`,
`AdsAuto`.`ad_colour_id`, `AdsAuto`.`capacity`, `AdsAuto`.`cilinders`,
`AdsAuto`.`power`, `AdsAuto`.`ad_gearbox_id`, `AdsAuto`.`gears`,
`AdsAuto`.`pricein`, `AdsAuto`.`priceout`, `AdsAuto`.`ta`,
`AdsAuto`.`milage`, `AdsAuto`.`comment`, `AdsAuto`.`name`,
`AdsAuto`.`phone`, `AdsAuto`.`email`, `AdsAuto`.`ad_country_id`,
`AdsAuto`.`ad_city_id` FROM `adverts` AS `Advert` LEFT JOIN
`ad_categories` AS `AdCategory` ON (`Advert`.`ad_category_id` =
`AdCategory`.`id`) LEFT JOIN `ads_autos` AS `AdsAuto` ON
(`AdsAuto`.`advert_id` = `Advert`.`id`) WHERE `AdImage`.`url` =
'test.jpg'
Error message:
SQL Error: 1054: Unknown column 'AdImage.url' in 'where clause'
AdImage (and AdvertComfort too) table don't join..
On Dec 17, 7:21 pm, Rob <[email protected]> wrote:
> The joins are automatically done for you if your models have the
> correct relationships defined.
>
> You'd need to define your hasMany relationships, and their
> corresponding belongsTo in your models.
>
> In your Advert model:
>
> // Link to signup slots and user groups ...
> var $hasMany = array(
> 'AdImage' => array(
> 'className' => 'AdImage',
> 'foreignKey' => 'advert_id',
> 'limit' => '5',
> 'dependent' => true
> ),
> 'AdvertComfort' => array(
> 'className' => 'AdvertComfort',
> 'foreignKey' => 'advert_id',
> 'limit' => '5',
> 'dependent' => true
> )
> );
>
> var $hasOne = array(
> 'AdsAuto' => array(
> 'className' => 'AdsAuto'
> 'foreignKey' => 'advert_id',
> 'dependent' => true
>
> );
>
> Then in your other models, you need the corresponding belongsTo back
> to Advert:
>
> var $belongsTo = array('Advert');
>
> Then when you do your queries, you should get the associated data (the
> hasMany sets the foreign key for the find, so all you need to add are
> your conditions).
>
> If your relationships get more complex, you may need to muck with
> $recursive:http://book.cakephp.org/view/439/recursive
>
> On Dec 16, 11:49 pm, dev <[email protected]> wrote:
>
> > Yes, find conditions not a big problem, but how to join table with
> > hasMany association (AdImage, and AdvertComfort)? After that, i coud
> > set conditions
>
> > On Dec 15, 9:46 pm, Rob <[email protected]> wrote:
>
> > > You shouldn't have any trouble doing this if you build your models
> > > correctly and choose the right conditions in your find statement in
> > > the controller.
>
> > > I personally tend to do this by building the SQL query that works, and
> > > then back coding the conditions into the controller (although I'm sure
> > > there are lots of ways to come up with the same thing.
>
> > > I would think you'd have something like the following (untested
> > > obviously):
>
> > > $adverts = $this->Advert->find('all', 'conditions' => array( 'AND' =>
> > > array(
> > > 'AdsAuto.year BETWEEN ? AND ?' => array($date1,
> > > $date2),
> > > '(select count(1) from ad_images AdImage where
> > > AdImage.advert_id = Advert.id > 0)',
> > > 'AdvertComfort.option_id' => array(1,7,8)
> > > );
>
> > > On Dec 14, 4:38 pm, dev <[email protected]> wrote:
>
> > > > Hey,
>
> > > > I'm trying to build find function for many days with no luck, please
> > > > help me guys.
>
> > > > Models:
>
> > > > mysql> DESCRIBE adverts;
> > > > +----------------+------------------------+------+-----+---------
> > > > +----------------+
> > > > | Field | Type | Null | Key | Default |
> > > > Extra |
> > > > +----------------+------------------------+------+-----+---------
> > > > +----------------+
> > > > | id
> > > > | user_id
> > > > | ad_category_id
> > > > | code
> > > > | valid
> > > > | bad
> > > > | created
> > > > +----------------+------------------------+------+-----+---------
> > > > +----------------+
>
> > > > mysql> DESCRIBE ads_autos;
> > > > +--------------------+------------------+------+-----+---------
> > > > +----------------+
> > > > | Field | Type | Null | Key | Default |
> > > > Extra |
> > > > +--------------------+------------------+------+-----+---------
> > > > +----------------+
> > > > | id
> > > > | advert_id
> > > > | ad_manufacturer_id
> > > > | ad_model_id
> > > > | ad_type_id
> > > > | year
> > > > | ad_fueltype_id
> > > > | ad_door_id
> > > > | ad_colour_id
> > > > | capacity
> > > > | cilinders
> > > > | power
> > > > | ad_gearbox_id
> > > > | gears
> > > > | pricein
> > > > | priceout
> > > > | ta
> > > > | milage
> > > > | comment
> > > > | name
> > > > | phone
> > > > | email
> > > > | ad_country_id
> > > > | ad_city_id
> > > > +--------------------+------------------+------+-----+---------
> > > > +----------------+
>
> > > > mysql> DESCRIBE ad_images;
> > > > +-----------+------------------+------+-----+---------+----------------
> > > > +
> > > > | Field | Type | Null | Key | Default | Extra
> > > > |
> > > > +-----------+------------------+------+-----+---------+----------------
> > > > +
> > > > | id
> > > > | advert_id
> > > > | url
> > > > | created
> > > > +-----------+------------------+------+-----+---------+----------------
> > > > +
>
> > > > mysql> DESCRIBE advert_comfort;
> > > > +-----------+------------------+------+-----+---------+----------------
> > > > +
> > > > | Field | Type | Null | Key | Default | Extra
> > > > |
> > > > +-----------+------------------+------+-----+---------+----------------
> > > > +
> > > > | id
> > > > | advert_id
> > > > | option_id
> > > > +-----------+------------------+------+-----+---------+----------------
> > > > +
>
> > > > Models and associations:
>
> > > > Advert hasMany AdImage
> > > > Advert hasMany AdvertComfort
> > > > Advert hasOne AdsAuto
>
> > > > I need find function, when there are such conditions, for example:
>
> > > > AdsAuto.year from .. to ..
> > > > AdvertComfort.option_id is 1,7,8 ...
> > > > and Advert has at least one AdImage (photo)
>
> > > > I tried many ways with Containable, INNER JOIN but with no luck. I
> > > > can't get all these conditions in one query, but i need to get Advert
> > > > model results if only all conditions are true.
>
> > > > P.S. sorry for my bad english
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---