简体   繁体   中英

How to retrieve data from two related tables using cakephp?

I have two tables in a database, one as event(eventID,name,location), and the other one as eventImages(id,eventID,path).

I need to get the images related to each event

I have tried the following statement in CakePHP:

<?php
   App::uses('AppModel','Model');
   class EventImages extends AppModel {
      public $belongsTo = array('Event' => array('className' => 'Event','foreignKey' => 'eventID'));
 }

but no data is retrieved in the controller any missing statement?

You have to define relation in Event model

 public $hasMany = array('EventImages'=>array('className'=> 'EventImages', 'foreignKey'=>'eventID') //for multiple image

According to naming convention in cakephp Model name should be singular. Also you have to define public $primarykey = 'eventID' in Event model as cakephp by default use id field as primary key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM