$add = new Model_Organisation(); $add->user_id = self::$user_id; $add->type = 'O'; // Organisation, churches added elsewhere $add->name = Input::post('name'); $add->addresses[] = new Model_Address(); $add->addresses->user_id = $add->user_id; $add->addresses->org_id = $add->id; $add->addresses->post_code = Input::post('post_code'); $add->addresses->address_1 = Input::post('address_1'); $add->addresses->address_2 = Input::post('address_2'); $add->addresses->town = Input::post('town'); $add->addresses->county = Input::post('county'); $add->addresses->email = Input::post('email'); $add->addresses->phone = Input::post('phone'); $add->addresses->mobile = Input::post('mobile'); $add->addresses->website_uri = Input::post('website_uri'); $add->addresses->facebook_uri = Input::post('facebook_uri'); $add->addresses->twitter_uri = Input::post('twitter_uri'); $add->address_id = $add->addresses->id; $add->description = Input::post('description'); $add->save();
protected static $_properties = array( 'id', 'user_id', 'type', 'name', 'slug', 'address_id', 'description', ); // Set relationships protected static $_belongs_to = array( 'user' => array( 'key_from' => 'user_id', 'model_to' => 'Model_User', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, ) ); protected static $_has_one = array( 'address' => array( 'key_from' => 'address_id', 'model_to' => 'Model_Address', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, ) ); protected static $_has_many = array( 'event' => array( 'key_from' => 'id', 'model_to' => 'Model_Event', 'key_to' => 'org_id', 'cascade_save' => true, 'cascade_delete' => false, ), 'addresses' => array( 'key_from' => 'id', 'model_to' => 'Model_Address', 'key_to' => 'org_id', 'cascade_save' => true, 'cascade_delete' => false, ), );
protected static $_properties = array( 'id', 'user_id', 'org_id', 'post_code', 'address_1', 'address_2', 'town', 'county', 'email', 'phone', 'mobile', 'website_uri', 'facebook_uri', 'twitter_uri', ); // Set any relationships protected static $_belongs_to = array( 'users' => array( 'key_from' => 'user_id', 'model_to' => 'Model_User', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, ), 'organisations' => array( 'key_from' => 'org_id', 'model_to' => 'Model_Organisation', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, ), 'organisation' => array( 'key_from' => 'id', 'model_to' => 'Model_Organisation', 'key_to' => 'address_id', 'cascade_save' => true, 'cascade_delete' => false, ), 'event' => array( 'key_from' => 'id', 'model_to' => 'Model_Event', 'key_to' => 'address_id', 'cascade_save' => true, 'cascade_delete' => false, ) );
$val = Model_Organisation::validate('organisation'); if ($val->run()) { // validation successful, try and save the details $org = new Model_Organisation(); $org->user_id = self::$user_id; $org->type = 'O'; // Organisation, churches added elsewhere $org->name = Input::post('name'); $org->description = Input::post('description'); $org->address[] = new Model_Address(); $org->address->user_id = $org->user_id; //->user_id = self::$user_id; $org->address->is_default = true; $org->address->post_code = Input::post('post_code'); $org->address->address_1 = Input::post('address_1'); $org->address->address_2 = Input::post('address_2'); $org->address->town = Input::post('town'); $org->address->county = Input::post('county'); $org->address->email = Input::post('email'); $org->address->phone = Input::post('phone'); $org->address->mobile = Input::post('mobile'); $org->address->website_uri = Input::post('website_uri'); $org->address->facebook_uri = Input::post('facebook_uri'); $org->address->twitter_uri = Input::post('twitter_uri'); if($org and $org->save()) { // success! Session::set_flash('success', 'Organisation successfully added.'); Response::redirect('user/account'); } else { // oops, we couldn't save it Session::set_flash('error', 'Could not add organisation.'); } } Org: protected static $_many_many = array( 'address' => array( 'key_from' => 'id', 'key_through_from' => 'address_id', 'table_through' => 'addresses_organisations', 'key_through_to' => 'organisation_id', 'model_to' => 'Model_Address', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, ) ); Add: protected static $_many_many = array( 'organisation' => array( 'key_from' => 'id', 'key_through_from' => 'organisation_id', 'table_through' => 'addresses_organisations', 'key_through_to' => 'address_id', 'model_to' => 'Model_Organisation', 'key_to' => 'id', 'cascade_save' => true, 'cascade_delete' => false, ) ); The Error when reading (this worked before the change to many/many): Notice! ErrorException [ Notice ]: Undefined index: town APPPATH/views/user/account/organisation.php @ line 8: 7: <li><?php echo $org->name; ?></li> 8: <li><?php echo $org->address['town']; ?></li>
$org->address[0]->field_name = valueIs there a better way to do this? Would I be better passing in the array as a property when I create the new address object? If I print_r() the object I am trying to read, I can see an empty array for address. Is this a problem with my relationship or with the query I am performing? As an aside, it seems to be ignoring my validation for address, do I have to call a separate validation instance on it?
$address = current($org->address);
$org->address[] = new Model_Address(); $org->address->user_id = $org->user_id;In a many-to many, the relation is an array of objects, so the first line is correct (you create a new array element). But in the second line you access the array as an object? Better do
$org->address[] = new Model_Address( array('user_id' => $org->user_id) );to create a new object.
It looks like you're new here. If you want to get involved, click one of these buttons!