1

I am trying to make the relationship between the tables I read The documentations in cakephp3, and tons of post in stackoverflow, and still can’t get any result. I have two tables.

  • category (id, name, published, created, modified)

  • portfolio (id, category_id, name, created, modified)

I want to display every element of portofolio with category like this:

Category Cars

* element 1: Bmw
* element 2: Audi
* element 3: Ford

How can i make this relation and display?

In categories model I write :

 $this->belongsTo('Portfolios', [
              'foreignKey' => 'id'
          ]);

And this is the query:

$portfolios_category = $this->Categories->find()
            ->contain(['Portfolios'])
            ->where(['Categories.published' => 1])
            ->order(['Categories.created' => 'desc'])
            ->all();
ndm
  • 59,784
  • 9
  • 71
  • 110
Frost BYTE
  • 11
  • 1

2 Answers2

1

Just one correction :

 $this->belongsTo('Portfolios', [
              'foreignKey' => 'category_id'
 ]);

After that loop through $portfolios_category , hope you will get the result.

Shifat
  • 732
  • 1
  • 6
  • 20
0

Start using the command prompt. Go to your project_folder/bin/ and open command prompt.

Then type

cake bake model

then you will get the list of Table with model name, then type

cake bake model <ModelName>

ex:

cake bake model Category
cake bake model Portfolio 
RiTeSh
  • 513
  • 3
  • 12