Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Change 'key_to' dynamically
  • Hello, I am trying to store orders that have an invoice address and a shipping address. I have an address model and an order model and I need the order model to save 'shipping_address_id' and 'invoice_address_id'. By default the address model's 'key_to' is 'address_id' but I need to dynamically change it to 'invoice_address_id' when the controller creates the invoice instance of the address model and 'shipping_address_id' when the controller creates the shipping instance of the address model. How would I go about doing this? Thanks.
  • You can just define two relationships to the same other model, one using the shipping_address_id' as foreign key, the other using 'the invoice_address_id'. Something like
    protected static $_belongs_to = array(
        'shipping_address' => array(
            'model_to' => 'Model_Address',
            'key_from' => 'shipping_address_id',
        ),
        'invoice_address' => array(
            'model_to' => 'Model_Address',
            'key_from' => 'invoice_address_id',
        ),
    );
    
  • Thanks for your reply Harro. I have been fighting with this all day but can't get it to work. It was late when I posted my question and I may not have explained what I was trying to do very well. Here is the code I am using. Contoller:
    $customer = Model_Customer::forge(array(
        'first_name' => Input::post('first_name'),
        'last_name' => Input::post('last_name'),
    ));
        
    $customer->address['invoice'] = Model_Address::forge(array(
         'address' => Input::post('address'),
         'address2' => Input::post('address2'),
         'city' => Input::post('city'),
         'state' => Input::post('state'),
         'zip' => Input::post('zip'),
    ));
        
    $customer->address['shipping'] = Model_Address::forge(array(
          'address' => Input::post('shipping_address'),
          'address2' => Input::post('shipping_address2'),
          'city' => Input::post('shipping_city'),
          'state' => Input::post('shipping_state'),
          'zip' => Input::post('shipping_zip'),
     ));
        
    $customer->order[] = Model_Order::forge(array(
        'total_cost' => $total_cost,
    ));
    
    $customer->save()
    
    Customer Model
    class Model_Customer extends \Orm\Model
    {
     
     protected static $_properties = array(
      'id',
      'first_name',
      'last_name',
     );
    
     protected static $_has_many = array('order','address'); 
    
    }
    

    Address Model
    class Model_Address extends \Orm\Model
    {
     
     protected static $_properties = array(
      'id',
      'customer_id',
      'first_name',
      'last_name',
      'address',
      'address2',
      'city',
      'state',
      'zip',
     );
    
     protected static $_has_many = array('order'); 
     protected static $_belongs_to = array('customer');
    }
    

    Order Model
    class Model_Order extends \Orm\Model
    {
     protected static $_properties = array(
      'id',
      'customer_id',
      'invoice_address_id',
      'shipping_address_id',
      'total_cost',
     );
     
     protected static $_belongs_to = array(
      'customer',
      'shipping_address' => array(
       'model_to' => 'Model_Address',
       'key_from' => 'shipping_address_id',
      ),
      'invoice_address' => array(
       'model_to' => 'Model_Address',
       'key_from' => 'invoice_address_id',
      ),
     );
    }
    

    The address model is called from the controller twice and the id's generated from each need to be saved in the one order record. Is this something that can be done with the ORM package? Thanks again.
  • That is inconsistent with your earlier message. You don't give the Customer model but if that has the FK for both the shipping and the invoice address, then there's more than one thing wrong. First, if the addresses posted are new, then you need to create them first. In your example you forge an new address, but you never save it. So as far as the ORM is concerned, it doesn't exist, so it can't be related. If they are not new, you can't forge them, you need to look them up. Second, "$customer->address" doesn't make any sense. Assuming "address" is a defined relation of customer, you need to use id's. But assuming there's only one invoice address, "address" is not an array. And why do you assign the addresses to the customer, but define the relations on Model_Order?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion