Domanda

In mie citazioni app belongTo un prodotto, che a sua volta appartiene ad un materiale. Come non posso ottenere il modello afterFind matrice prodotto per includere il materiale quando si accede dal modello Quota ho associato il preventivo direttamente con un materiale.

Il problema che sto avendo ora è che il material_id per la citazione ha bisogno di essere salvati automaticamente in base al prodotto che viene selezionato per la citazione

vale a dire. tirando il valore della Product.material_id dal prodotto selezionato e salvarlo al campo Quote.material_id automaticamente prima che la citazione è stato salvato nel database.

Sono abbastanza nuovo per cakePHP. Qualcuno sa come questo può essere fatto?

EDIT:

Ecco un esempio per aiutare a spiegare. Nel mio modello Citazione posso avere:

public function beforeSave($options) {
    $this->data['Quote']['material_id'] = 4;
    return true;
}

, ma ho bisogno di fare qualcosa di più come questo che non funziona:

public function beforeSave($options) {
    $this->data['Quote']['material_id'] = $this->Product['material_id'];
    return true;
}
È stato utile?

Soluzione

I'm shocked this hasn't been properly answered yet....

Oldskool's response is semi-correct, but not entirely right. The use of "$this->Quote" is incorrect, as the beforeSave function itself resides in the Quote class. I'll explain using an example.

-> We have a model Subscription which belongsTo a SubscriptionsPlan

-> Model SubscriptionsPlan hasMany Suscriptions

To access the SubscriptionsPlan data in a beforeSave function in the Subscription model, you would do the following:

public function beforeSave($options = array()){
    $options = array(
        'conditions' => array(
            'SubscriptionsPlan.subscriptions_plan_id' => $this->data[$this->alias]['subscriptions_plan_id']
        )
    );

    $plan = $this->SubscriptionsPlan->find('first', $options);

    //REST OF BEFORE SAVE CODE GOES HERE
    return true;
}

Altri suggerimenti

It should probably work by using a find instead.

public function beforeSave($options) {
    // Assuming your Product model is associated with your Quote model
    $product = $this->Quote->Product->find('first', array(
        'conditions' => array(
            'Product.material_id' => $this->data['Quote']['material_id']
        )
    ));
    $this->data['Quote']['material_id'] = $product['material_id'];
    return true;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top