Welcome Guest, Not a member yet? Register   Sign In
[DataMapper] (1.7.1) Relation saving issue
#1

[eluser]grisha[/eluser]
Hello there. Today I've tried to implement DM 1.7.1 into one of my projects. Following the guide provided by DM developer I've encountered a problem.

The Contractor Creation method is like this:

Code:
$c = new CContractor();

$c->company_name = $this->input->post('company_name');
// ...
$c->comment = $this->input->post('comment');

// Store the contractor
$c->save();

// Relate him with the current customer
$this->customer->save($c);

The
Code:
$this->customer
field is a member of MY_Controller and set up during the MY_Controller::__construct() with a valid Customer class object.

The contractor is being added properly to the database. Just before the call of
Code:
$this->customer->save($c)
I've checked the ID's of both $this->customer and $c - and they are both valid integers - so that means that objects are valid.

I'm not sure if it isn't a bug or smth. In the end I've got:
Code:
Unable to relate customer with ccontractor.

Am I doing something wrong?
#2

[eluser]WanWizard[/eluser]
You get this message if you don't have a valid relationship defined between both models (note: you have to do this in BOTH models).

Show me the relationship definitions from both the Ccontractor and the Customer model if you want me to have a look.
#3

[eluser]grisha[/eluser]
The relation is defined by the code fragments listed below:

file: models/crm/customer.php

Code:
class Customer extends DataMapper
{
    var $has_many = array('CFile', 'CContractor', 'CProduct');

file: models/customer/company/ccontractor.php

Code:
class CContractor extends DataMapper
{
    var $has_one = array('Customer');
#4

[eluser]sooner[/eluser]
Class names must have the first letter capitalized with the rest of the name lowercase..this is what i find in the models user guide of codeigniter...it may be because of that..but i am not sure..
#5

[eluser]WanWizard[/eluser]
That is correct. The lookup of the relation happens using the capitalized name of the model, unless you use an advanced relationship definition (with an array instead of a string), in which case the class name will be used as defined.

This this case, it can't find a relation for Customer called 'Ccontractor'.

Either define them as stated in the manual, or use an advanced relationship definition so you can name them whatever you want.
#6

[eluser]grisha[/eluser]
Looks like it is the solution Smile Thanks guys.

The code below is a example solution for this problem:

Code:
class Customer extends DataMapper
{
    var $has_many = array(
        'cfile' => array(
            'class' => 'CFile'
        ),
        'ccontractor' => array(
            'class' => 'CContractor'
        ),
        'cproduct' => array(
            'class' => 'CProduct'
        )
    );

Thanks once again.

I would recommend extending the error description text, so you can avoid such questions in future Smile
#7

[eluser]WanWizard[/eluser]
Well, I thought the message was self explanatory:
Quote:Unable to relate customer with ccontractor.
i.e. "Help, I can't find the relationship between the two".




Theme © iAndrew 2016 - Forum software by © MyBB