0

I have 2 table on model , Table1 and Table2 Column on Table1

ID | Content1

Column on Table2

ID | table1_id | Content2

i want to display Content in Table1 on Table2 , how to join column?

thanks advance!

subz
  • 19
  • 1
  • 7
  • 2
    You question is not clear. Also post your so far tried code. – Rikesh Feb 06 '14 at 05:17
  • Use find query and try to use Contain.. But the Question you asked is not clear. Need more info to help you – AnNaMaLaI Feb 06 '14 at 05:42
  • Do you want to join the result of both tables ? – Moyed Ansari Feb 06 '14 at 07:10
  • i actually resolve this problem rightnow :D thanks anyway for replied.. it simple.. i just put this on my source code public $belongsTo = array('Goods' => array('className'=>'Goods','foreignKey'=>'goods_id','dependent'=>true)); :D – subz Feb 06 '14 at 08:00

1 Answers1

2

Model\Model1.php

<?php
App::uses('AppModel', 'Model');

class Model1 extends AppModel {

    public $useTable = 'table1';

}

Model\Model2.php

<?php
App::uses('AppModel', 'Model');

class Model2 extends AppModel {

    public $useTable = 'table2';

    public $belongsTo = array(
        'Model1' => array(
            'className' => 'Model1',
            'foreignKey' => 'table1_id'
        )
    );

}

In the controller:

$data = $this->Model2->find('all');

The generated query will be

SELECT `Model2`.`id`, `Model2`.`table1_id`, `Model2`.`content2`, `Model1`.`id`, `Model1`.`content1` FROM `db`.`table2` AS `Model2` LEFT JOIN `cake244`.`table1` AS `Model1` ON (`Model2`.`table1_id` = `Model1`.`id`) WHERE 1 = 1
cornelb
  • 6,046
  • 3
  • 19
  • 30