Cakephp foreign key not primary key

I have a website developed in cakephp 2.0, I want to establish a relationship between two tables:

activity_ingredients

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   activity_id     int(11)     No  None        
4   ingredient_id   int(10)     No  None        
5   created     datetime        

Actions

1   id  int(10) UNSIGNED    No  None    AUTO_INCREMENT  
2   type_id     tinyint(2)  No  None        
3   language    char(2)     No  None        
4   text    varchar(100)        No  None        
5   created     datetime    

I want to associate two tables with the "type_id" field. I made my code in this mode:

    class Action extends AppModel{
    public $name = 'Action'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

    public $belongsTo = array(
        'ActivityIngredients' => array(
            'className'     => 'ActivityIngredients',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'type_id'
        )
    );

}

class ActivityIngredients extends AppModel{
        public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

        public $belongsTo = array(
            'Activity' => array(
                'className'     => 'Activity',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'activity_id'
            ),
            'Ingredient' => array(
                'className'     => 'Ingredient',
                'conditions'    => '',
                'order'         => '',
                'foreignKey'    => 'ingredient_id'
            )
        );

        public $hasMany = array(
            'Action' => array(
                'className' => 'Action',
                'conditions' => '',
                'dependent' => true,
                'foreignKey'   => 'type_id',
                'associatedKey'   => 'type_id'
            )
        );
    }

It does not retrieve the correct data. It seems to be accepting an identifier for a foreign key. This is the view:

<?php foreach ($user['Activity'] as $activities) {
var_dump($activities);
?>
    <div class="line-cnt"><div class="line">
    </div>
</div>
<h2>Attività</h2>
<div class="table">
    <div>
        <div>Activity created</div><div><?php echo $activities['created']; ?>
        </div>
    </div>
    <div>
        <div>Actions text</div><div><?php echo $activities['Action']['text']; ?></div>
    </div>
    <div>
        <div>ActivityIngredient ingredient_id</div><div><?php echo $activities['ActivityIngredients']['ingredient_id']; ?></div>
    </div>
</div>
<?php
}
?>

The controller is a simple query with all and recursive 3 searches in User, which is collegial with tables

$this->User->recursive = 3;
        $user = $this->User->read();

        if (empty($username) || $username != $user['User']['username']) {
            $this->redirect(array ('action'=>'view',$id,$user['User']['username']));
        }

        $this->set('user', $user);

help me please

+5
source share
2 answers

First of all, if you use the id field in your activity_ingredients table, you should use it as foreignKey in another table.

- , - .

type_id , type_id activity_ingredients, , ActivityIngredient :

class ActivityIngredients extends AppModel{
    public $primaryKey = 'type_id';
    public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

    public $belongsTo = array(
        'Activity' => array(
            'className'     => 'Activity',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'activity_id'
        ),
        'Ingredient' => array(
            'className'     => 'Ingredient',
            'conditions'    => '',
            'order'         => '',
            'foreignKey'    => 'ingredient_id'
        )
    );

    public $hasMany = array(
        'Action' => array(
            'className' => 'Action',
            'conditions' => '',
            'dependent' => true,
            'foreignKey'   => 'type_id',
            'associatedKey'   => 'type_id'
        )
    );
}

. , , .

"type_id" . .

class ActivityIngredients extends AppModel{
public $name = 'ActivityIngredients'; //non utilizzata nel sito è il nome del modello alla fine per migliorare la compatibilità

public $belongsTo = array(
    'Activity' => array(
        'className'     => 'Activity',
        'conditions'    => '',
        'order'         => '',
        'foreignKey'    => 'activity_id'
    ),
    'Ingredient' => array(
        'className'     => 'Ingredient',
        'conditions'    => '',
        'order'         => '',
        'foreignKey'    => 'ingredient_id'
    )
);

public $hasMany = array(
    'Action' => array(
        'className' => 'Action',
        'conditions' => '',
        'dependent' => true,
        'foreignKey'   => false,
        'finderQuery'   => 'select * from actions as `Action` where
                            `Action`.`type_id` = {$__cakeID__$} '
    )
);

}

, . , .

+2

cakephp . , .

'foreignKey'   => 'Classname.type_id'
-1

All Articles