[eluser]WanWizard[/eluser]
That should work without problem, providing there is an entry in the $has_one or $has_many arrays in the User model called 'products' (and you should define it in User, because that's where you want the relation to be checked).
[eluser]ricardocasares[/eluser]
Well, it is not working, I don't know where am I wrong.
My user model
Code: class User extends DataMapper {
var $has_many = array('product');
var $validate = array(
'products' => array(
'rules' => array('max_size' => 3)
)
);
}
Product model
Code: class Product extends DataMapper {
var $has_one = array('user');
}
The controller
Code: class Test extends CI_Controller {
function test_related_save() {
$p = new Product();
$p->name = "Test product";
$u = new User();
$u->where('username','usertest')->get();
echo $p->save($u);
}
}
Ok this code always returns 1, and creates the records in DB, no matter the validation constraint to 3 products per user limit.
I'm using DM 1.8.2, and CI 2.1.0, any ideas?
[eluser]drakeonfire[/eluser]
Hello ricardocasares,
I'm no expert, far from it, but with reference to this:
Code: class User extends DataMapper {
var $has_many = array('product');
var $validate = array(
'products' => array(
'rules' => array('max_size' => 3)
)
);
}
Should $validate be $validation? (I've only seen examples using $validation, but I may be wrong here and you may be able to use $validate).
Also when you refer to 'products' I think you need to refer to it as it's singular name (lowercase model name), so it would need to be 'product' - Again I may be wrong but hopefully it's worth a try :-).
Amended version:
Code: class User extends DataMapper {
var $has_many = array('product');
var $validation = array(
'product' => array(
'rules' => array('max_size' => 3)
)
);
}
Good luck.
[eluser]ricardocasares[/eluser]
Drake you are right, It's validation, but it's ok in my models, I just mess up here while writing the post.
These are the actual files in my project:
Code: class User extends DataMapper {
var $has_many = array('product');
var $validation = array(
'product' => array(
'rules' => array('max_size' => 3)
)
);
}
Code: class Product extends DataMapper {
var $has_one = array('user','measure');
var $has_many = array('movement');
var $validation = array(
'measure_id' => array(
'rules' => array('required'),
'label' => 'unit'
),
'name' => array(
'rules' => array('required'),
'label' => 'name'
),
'existence' => array(
'rules' => array('required','numeric'),
'label' => 'existence'
),
'threshold' => array(
'rules' => array('required','numeric'),
'label' => 'alert'
)
);
var $default_order_by = array('name' => 'asc');
function __construct($id = NULL)
{
parent::__construct($id);
}
}
[eluser]ricardocasares[/eluser]
Can't make it work, maybe it is the way I'm saving the objects?
Code: function test()
{
$u = new User(2);
$p = new Product();
$p->name = "PEPE";
$p->existence = 2;
$p->threshold = 1;
$p->measure_id = 3;
echo $p->save($u);
}
[eluser]ricardocasares[/eluser]
Well I'm getting this error now:
Code: A Database Error Occurred
Error Number: 1054
Unknown column 'products.product_id' in 'where clause'
SELECT COUNT(*) AS `numrows` FROM (`products`) WHERE `products`.`user_id` = 1 AND `products`.`product_id` NOT IN (309)
Filename: /var/www/stocker/libraries/datamapper.php
Line Number: 2596
This happens while trying to do this:
Code: $u = new User();
$p = new Product();
$p->name = "PEPE";
$p->existence = 2;
$p->threshold = 1;
$p->measure_id = 3;
$p->save();
$u->where('username','rcasares')->get();
echo $u->save($p);
Pleaaaseee, what I'm I doing wrong??
[eluser]ricardocasares[/eluser]
Ok, I got it all working now... Except, I need to do the following:
Code: var $validation = array(
'product' => array(
'label' => 'productos',
'rules' => array('required','max_size' => $products_limit)
)
);
The $products_limit comes from the "plan" the user has associated, and it's stored in the session when the user logs in. When I try to run this I get:
Code: Parse error: syntax error, unexpected T_VARIABLE in /var/www/stocker/application/models/user.php on line 11
Is there any way to make this setting dynamic?
[eluser]WanWizard[/eluser]
You can not put any code in a property assignment.
So you have to do that in your constructor, before you call the parent:
Code: $this->validation['product']['rules']['max_size'] = $products_limit;
[eluser]DerLola[/eluser]
Hi people, I've been using CI for quite a while but I'm new to ORMs, so please be.. gentle
I have a timelines table and an events table. Every event has a timelines_id foreign key. Now I want to retrieve a timeline and its event, here's my simplified code
Code: class Timeline extends DataMapper {
var $has_one = array();
var $has_many = array(
'event' => array(
'join_other_as' => 'events'
)
);
var $validation = array();
function __construct($id = NULL)
{
parent::__construct($id);
}
}
Code: class Events extends DataMapper {
var $has_one = array('timeline');
var $has_many = array();
var $validation = array();
function __construct($id = NULL)
{
parent::__construct($id);
}
}
And in the controller I say:
Code: $timeline = new Timeline($id);
$timeline->events->get();
Now how do I convert the result into a simple object (not the ORM object)? I need to use it in my template engine. I've looked at the to_array extension but that doesn't seem to do it. I want a timeline object containing a regular array filled with event objects.
Any help is much appreciated!
[eluser]WanWizard[/eluser]
Datamapper doesn't work with "regular" objects, it works with Datamapper objects. There is also a one-to-one relation between a record and an object, it's how the design pattern behind Datamapper works, so data from one table will never be in the object of another.
If you want a custom data structure, you can create an extension for it (similar to the array extension).
|