链接和显示蛋糕php 3中的表格中的数据

I am trying to display the names from the types table in my jobs/index.ctp

not sure how to echo this, so far I have written this

but it is not working but also not giving any error message

class Jobs extends Table
{

    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('jobs');
        $this->setDisplayField('title');
        $this->setPrimaryKey('id');

        $this->addBehavior('Timestamp');

        $this->belongsTo('Categories', [
            'foreignKey' => 'category_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Users', [
            'foreignKey' => 'user_id',
            'joinType' => 'INNER'
        ]);
        $this->belongsTo('Types', [
            'foreignKey' => 'type_id',
            'joinType' => 'INNER'
        ]);
    }
class Types extends Table
{
    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        parent::initialize($config);

        $this->setTable('types');
        $this->setDisplayField('name');
        $this->setPrimaryKey('id');

        $this->hasMany('Jobs', [
            'foreignKey' => 'type_id'
        ]);
    }
<?php
foreach ($jobs as $jobs):
?>
<li>
    <div class="type"><span style="background:"> <?php echo $jobs['types']['name']; ?> </span></div>
    <div class="description">
        <h5><?php echo $jobs['title']; ?> (<?php echo $jobs['city']; ?> , <?php echo $jobs['postcode']; ?>)</h5>
        <h6><strong>company Name: <?php echo $jobs['company_name']; ?></strong></h6>
        <h6>Date Posted:<span id="list_date"> <?php echo $this->Time->Format($jobs['created'], "d MMMM y") ?> </span></h6>

It all works apart from this line <?php echo $jobs['types']['name'] ;?>

First off, your foreach loop should be foreach ($jobs as $job). Note the singular $job for the variable that tracks each particular job.

Following on that singular note, since Jobs belongTo Types, there is only a single type for each job, not multiple. As a result, the value you're looking for is $job['type']['name'] ($job->type->name will likely also work). This all assumes that you've correctly used containment when loading the record; hence why I asked for that code to be shown too.

If Jobs had many Types, then you'd have $job['types'] (or $job->types), which would be an array of Type entities that you could iterate over.

For displaying the associated table data you need 3 things:

  1. Association in Table,

    $this->belongsTo('Types', [
        'foreignKey' => 'type_id',
        'joinType' => 'INNER'
    ]);
    
  2. In controller (JobsController) specify the associated table using contain,

    public function index() {
        $jobs = $this->Jobs->find('all')->contain(['Types']);
        //set jobs variable to make it available in .ctp file 
    }
    
  3. In Jobs/index.ctp file

    <?php echo $job['types']['name'] ;?>