Welcome Guest, Not a member yet? Register   Sign In
DataMapper ORM v1.8.2

[eluser]wisepotato[/eluser]
Yes wanwizard, but the Get-rules don't apply in all situations. Can i create new variables with it. And tarciozemel, do I have to create a seperate model for extra models? That would be kinda awkward.

[eluser]WanWizard[/eluser]
Get rules run when you read data from the database, I use them for example to convert an array stored in the database serialized back to it's original form.

Normal rules run when you save, which can be used to provide defaults for fields not entered. In the same example, I use a rule that serializes properties that are an array before saving the data.

You can define a custom method as a rule, and in that method, you can do whatever you want, you have full access to the model object.

If you want to pre-populate the object after creation, use the post_model_init() method.

[eluser]wisepotato[/eluser]
So for example i could call one method in the get_rules and then add like 5 variables for easy access? Will that work in every context? (did you make this btw? It rules.)

It just seems like a bit of hack.

[eluser]WanWizard[/eluser]
You can do whatever you want in that method.

And yes, that is a hack. But so is creating properties that aren't part of the object. Which was the first comment you got.

Use validation rules to set defaults for fields that are not set, and throw errors for fields that don't have a default. If you need data that is related to field values, make a custom method for it in the model, and have that return the correct value based on the current field values.

Today you hack a non-existent property into the object, tomorrow you decide to add a new column to the model with the same name as that property, and you're in deep trouble...

[eluser]wisepotato[/eluser]
Ah. So it is not the way to go.

Is my way of thinking wrong? I'd like to use your lib the way it is intended.

[eluser]WanWizard[/eluser]
Indeed.

Adding new properties in general is not a good idea, as it might interfere with future use of the model.

In cases where I have a need for it, I create a property "protected $_custom = array()", and use setters and getters to work with values in this array. This way you can add custom data as much as you want, without tainting the object.

You can use the rules and get rules to populate this property if it's values are related to data you read or write.

Code:
class Modelname extends Datamapper
{
    protected $_custom = array();

    public function get_custom($name)
    {
        return isset($this->_custom[$name]) ? $this->_custom[$name] : NULL;
    }


    public function set_custom($name, $value = null)
    {
        if ($value === NULL)
        {
            if (isset($this->_custom[$name]))
            {
                unset($this->_custom[$name]);
            }
        }
        else
        {
            $this->_custom[$name] = $value;
        {
    }
}

[eluser]ahmetkapikiran[/eluser]
Hello Wanwizard,
I have include_related functions problem.

I have two models: Video and Category.

Video models:
Code:
Class Video extends DataMapper {
    var $table = 'video';
var $has_one = array('category'=>array('class'=>'category','other_field'=>'video','join_table'=>'category'),'user'=>array('class'=>'user','other_field'=>'video','join_table'=>'user'));
var $has_many = array();  
    
    function get_seo_link(){
        return site_url($this->category_seo_link.'/'.$this->seo_link.'.html');
    }
}
Category Models:
Code:
class Category extends DataMapper {
    var $table = 'category';
var $has_one = array();
var $has_many = array('video'=>array('class'=>'video','other_field'=>'category','join_table'=>'video'));
    
    function get_seo_link(){
        return site_url($this->seo_link);
    }  
}

My Controller Code:
Code:
$vg = new Video();
$vg->include_related('category');
$vg->where('id',1);
$vg->get();

//Working
echo 'Video Link: '.$vg->get_seo_link();

//get_seo_link function working. But variable with in function empty :(
echo 'Category Link: '.$vg->category->get_seo_link();

How can I do?

[eluser]WanWizard[/eluser]
include_related() doesn't auto-fetch related object, it does what it says on the tin, it includes them (in SQL terms, it does a JOIN).

So no $vg->category object will be created, the columns of the related category record will be included in the video object, as properties prefixed with "category_".

You can use the "auto_populate" => TRUE on the relation to have Datamapper auto-fetch the related object (but that it will always fire two queries on each get).

[eluser]ahmetkapikiran[/eluser]
[quote author="WanWizard" date="1338826695"]include_related() doesn't auto-fetch related object, it does what it says on the tin, it includes them (in SQL terms, it does a JOIN).

So no $vg->category object will be created, the columns of the related category record will be included in the video object, as properties prefixed with "category_".

You can use the "auto_populate" => TRUE on the relation to have Datamapper auto-fetch the related object (but that it will always fire two queries on each get).[/quote]
Thank u Wanwizard. I solved to auto_populate.

[eluser]animatora[/eluser]
Hi, I am having an issue with the ORM validation. I am using the CI form_validation as well,this is where the problem comes from. I am including the form validation in my autoload.php. Whenever I try to run validation on model level I am getting this:

Code:
Unable to access an error message corresponding to your rule name: required.

Any ideas how to fix it, so I can keep the CI form_validation ? ORM Datamapper is 1.8.2

Cheers




Theme © iAndrew 2016 - Forum software by © MyBB