Welcome Guest, Not a member yet? Register   Sign In
DMZ 1.7.1 (DataMapper OverZealous Edition)

[eluser]tomdelonge[/eluser]
How can you search on a relationship?

For example, if I have posts and each post as one author, searching for "ada" should return any posts written by "adam". Maybe this isn't the best example...

But I need to search on a table where each record has one in another table (I need to search on the "other" table's columns)

[eluser]OverZealous[/eluser]
@tomdelonge
Please see the where_related functions on the Get Advanced page.
Ex:
Code:
$post->like_related('user', 'ada', 'after')->get();

[eluser]WanWizard[/eluser]
Phil,

Thanks to all the work you've put in to make DMZ a great tool to work with!

As I understand, two other people besides me have come forward to take it over from you. I personally feel that forking this three-ways isn't very productive, so I think the three of us have to see how we are going to move on with Datamapper. Any suggestions?

[eluser]defeed[/eluser]
I'm adding a new movie and there is a text input to enter genre. How can I check if the given genre already exists in the 'genres' table thus relate a new movie to that genre by '$movie->save($genre)', otherwise add a new genre and then relate it to the movie by doing '$genre->save(); $movie->save($genre)'? Anyone, please?

[eluser]WanWizard[/eluser]
Something like
Code:
$genre = new genre();
$genre->get_by_name( $name );
if ( ! $genre->exists() )
{
    $genre->name = $name;
    $genre->save();
}
$movie->save($genre);

[eluser]defeed[/eluser]
Thanks, it worked! And maybe the last question (sorry for abusing youSmile
If I enter several genres delimited by comma, how could I break this string to a separate genres and relate them all to a movie, creating non-existent genres?

[eluser]alaminx[/eluser]
I have some trouble in save related data:
The country have many city
city has country_id key
When I try to save country to city. The data saved in country_id field is NULL.
This is model:
Code:
<?php
class Country extends DataMapper
{
    public $table="countries";
    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    public $has_many=array(
            'city',
            'experience',
            'education');
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------
    public $validation=array(
        'name'=>array(
            'label'=>'Tên của quốc gia',
            'rules'=>array('required','trim','max_length'=>100))
        );
    function __construct($id=NULL)
    {
        parent::__construct($id);
    }
    function up_position()
    {  
        $max=new Country();
        $max->select_max('position');
        $max->where('position <' ,$this->position);
        $max->get();
        $o=new country();
        $o->where('position',$max->position);
        $o->get();    
        $ci =& get_instance();
        $ci->firephp->log($o->get_sql(),$o->name);
        if($o->result_count() >0 )
        {
            $tg = $this->position;
            $this->position = $o->position;
            $o->position=$tg;
            $o->save();
          
          
            $this->save();
            return true;
        }
        else
        {
            return false;
        }
    }
    function down_position()
    {
        $min=new Country();
        $min->select_min('position');
        $min->where('position >' ,$this->position);
        $min->get();
        $o=new country();
        $o->where('position',$min->position);
        $o->get();    
        $ci =& get_instance();
        $ci->firephp->log($o->get_sql(),$o->name);
        if($o->result_count() >0 )
        {
            $tg = $this->position;
            $this->position = $o->position;
            $o->position=$tg;
            $o->save();
            $this->save();
            return true;
        }
        else
        {
            return false;
        }
    }
    function save()
    {
        if(!$this->exists())
        {          
            $o = new Country();
            $o->select_max('position');
            $o->get();
            if(count($o->all)!=0)
            {
                $max=$o->position+1;
                $this->position=$max;
            }
            else
            {
                $this->postion = 1;
            }
        }
       return parent::save();
    }
}
/* End of file country.php */
/* Location: ./application/models/country.php */
Code:
&lt;?php
class City extends DataMapper
{
    public $table = "cities";
    // --------------------------------------------------------------------
    // Relationships
    // --------------------------------------------------------------------
    public $has_one = array('country');
    public $has_many = array('district');
    // --------------------------------------------------------------------
    // Validation
    // --------------------------------------------------------------------
    public $validation = array('name' => array('label' => 'Tên thành phố', 'rules' =>
        array('required', 'trim', 'max_length' => 100)));
    function __construct($id = null)
    {
        parent::__construct($id);
    }

    
    /********************************
    * Override the save method
    * check if new insert the position property
    **********************************/
    function save()
    {
        if (!$this->exists()) {
            $o = new city();
            $o->select_max('position');
            $o->get();
            if (count($o->all) != 0) {
                $max = $o->position + 1;
                $this->position = $max;
            } else {
                $this->postion = 1;
            }
        }
        return parent::save();
    }
}
/* End of file city.php */
/* Location: ./application/models/city.php */

And the add method:
Code:
function add()
    {
        $country_id=$this->uri->segment(4);
        //if request is GET
        $country=new country($country_id);
        
        if($_SERVER['REQUEST_METHOD']=="GET")
        {
            $city=new city();  
        }
        //if request if POST
        else
        {
            $city=new city();
            $city->name=$this->input->post('name');
          
            if($city->save($country))
            {
                  
               redirect($this->admin.'countries/list_cities/'.$country_id);    
            }
        }
         $dis['base_url']=base_url();
         $dis['country']=$country;
         $dis['menu_active']="Country";
         $dis['title']="Add New City To {$country->name}";
         $dis['object']=$city;
         $dis['view']='city/add';
         $this->viewadmin($dis);
    }

Help me please !

[eluser]WanWizard[/eluser]
You're overloading the save() method of the Datamapper object. Not a problem, but take into account that the save method can have parameters, which you ignore here.

So if you do this, it won't work:
Code:
$county = new Country(1);
$city = new City();
$city->name = 'test';
// parameter is ignored by your model!
$city->save($country);;

[eluser]alaminx[/eluser]
oh. I'm fool. Forget the save method's parameters. Thanks so much !

[eluser]johlin[/eluser]
I've just moved to DMZ from ignitedrecord and I really like how the validation is integrated, but I can't quite figure out how to make my relationships work.

I have on table for users and one for orders. The order table has got a field called userid which cannot be changed (the database is already in use by another application). Also, I can't quite figure out how to choose between a belongs_to and has_one-relationship.

How would I set that relation up so that a user can have many orders and orders belong to one user?




Theme © iAndrew 2016 - Forum software by © MyBB