0
votes

I'm trying to save an Order entity, but patchEntity always set two fields that are foreign keys to null.

Orders are associated to Addresses with 2 associations (Delivery and Invoice).

Associated addresses already exists, so I just want to save address id as a foreign key into Orders table.

OrdersTable

namespace OrderManager\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use OrderManager\Model\Entity\Order;

/**
 * Orders Model
 */
class OrdersTable extends Table {

/**
 * Initialize method
 *
 * @param array $config The configuration for the Table.
 * @return void
 */
public function initialize(array $config) {
    $this->table('orders');
    $this->displayField('id');
    $this->primaryKey('id');
    $this->addBehavior('Timestamp');

    $this->belongsTo('Contacts', [
        'foreignKey' => 'contact_id',
        'joinType' => 'INNER',
        'className' => 'ContactManager.Contacts'
    ]);

    // ... 

    $this->belongsTo('DeliveryAddresses', [
        'foreignKey' => 'delivery_address',
        'className' => 'ContactManager.Addresses'
    ]);
    
    $this->belongsTo('InvoiceAddresses', [
        'foreignKey' => 'invoice_address',
        'className' => 'ContactManager.Addresses'
    ]);
}

public function validationDefault(Validator $validator) {
    
    // ...

    $validator
            ->add('delivery_address', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('delivery_address');

    $validator
            ->add('invoice_address', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('invoice_address');
    
    // ...
}

Controller

$data = [
    // ...
    'contact_id' => 34,
    'delivery_address' => 8,
    'invoice_address' => 8,
    'currency' => 'Euro',
    'total_paid' => '100.00',
    'shipping_number' => ''
    // ...
];

$entity = $this->Orders->newEntity();
$entity = $this->Orders->patchEntity($entity, $data);
debug($entity);

debug($entity) always tells me :

'delivery_address' => null,

'invoice_address' => null,

When I remove the belongsTo associations (DeliveryAddresses and InvoiceAddresses), my fields get the numeric value (8). But I need these associations.

How can I keep these associations and save numeric values for the foreign keys ?

1
What does the _accessible property look like in your Order entity?Greg Schmidt
The _accessible property says delivery_address and invoice_addresse are accessible.iamsaloc

1 Answers

3
votes

The foreign key names are conflicting with the association property names (where the data for the associations is being stored), which are by default being derived from the association name, and in case of a belongsTo one it's the singular underscored variant of the association name, ie delivery_address and invoice_address.

See Cookbook > Database Access & ORM > Associations > BelongsTo Associations

To fix this, either stick to the conventions and append _id to your foreign keys, ie delivery_address_id and invoice_address_id, or change the property names using the propertyName option

$this->belongsTo('DeliveryAddresses', [
    'propertyName' => 'delivery_address_data',
    //...
]);

$this->belongsTo('InvoiceAddresses', [
    'propertyName' => 'invoice_address_data',
    //...
]);

Unless you're working with a legacy database, I'd strongly recommend to choose the former solution and make your foreign keys stick to the conventions!