Has One

Let's says that we have a Contact that only can have one country assigned.

In model/contact.php

class Contact extends ActiveRecord
{
	var $belongs_to = array('country'=>array('foreign_key'=>'country_id'));		 
}

In model/country.php

class Country extends ActiveRecord
{
	var $has_many = 'contacts';	 
}

Then in the controller/contact.php

    function show()
    {  		
 	$this->Contact->country->load(); 
    }

Now in contact.show.tpl we just show the country

          {contact.country.name}
 
has_one.txt · Last modified: 2008/09/19 17:45 by 70.75.42.247