@Benedikt
Just so you know - you can use where_related, you just have to call it after include_join_fields, like so:
$n->include_join_fields()->where_related($x)->get_iterated()This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.
The active forums are here.
March 14, 2010 11:43pm
Subscribe [104]#331 / Jun 09, 2010 10:22am
@Benedikt
Just so you know - you can use where_related, you just have to call it after include_join_fields, like so:
$n->include_join_fields()->where_related($x)->get_iterated()#332 / Jun 09, 2010 10:39am
Ah, ok. Thanks, that was not clear to me.
#333 / Jun 14, 2010 1:30pm
Hi,
I think I am going to extend DMZ using the PHP native inheritance mechanism (ie class DataMapperExt extends DataMapper {).
Line 421 of DMZ rev 395 there are these lines :
$this_class = strtolower(get_class($this));
$is_dmz = $this_class == 'datamapper';This means, that for my object that will instanciate my DataMapperExt class, $is_dmz will be set to false. Thus _load_languages() method will not be called. So I will have to call _load_languages manually in the constructor of my child class.
In my opinion, line 421 should be replaced by :
$is_dmz = $this instanceof DataMapper;
OR
$is_dmz = is_a($this, ‘DataMapper’);
It shouldn’t break anything.
#334 / Jun 14, 2010 1:44pm
@jpi
I also extend DataMapper, and there’s no need to do what you’re proposing. When CI loads the library ($this->load->library(‘datamapper’) or in your autoload config), the DataMapper class is instantiated and that code runs as it should. If you make your change, then you will run code for every DMZ model instantiation that only needs to run once when the library itself is loaded.
#335 / Jun 14, 2010 2:09pm
I agree because you said :
When CI loads the library ($this->load->library(‘datamapper’) or in your autoload config), the DataMapper class is instantiated
What if I want to do $this->load->library(‘DataMapperExt’) (my child class) or autoload DataMapperExt in my config file ? I think that scenario makes sense and in that case a problem will arise, but I am not sure since I have not tested yet.
Edit : Humm I see now what you meant by
If you make your change, then you will run code for every DMZ model instantiation that only needs to run once when the library itself is loaded.
This means that, I have no other option than to autoload the original DataMapper and then make my model extends DataMapperExt. It’s a bit weird but it’s okay.
#336 / Jun 14, 2010 2:52pm
In my case, what I actually do is use a CI pre-system hook to set up my own custom autoloader. If something extends my equivalent of your DataMapperExt, then when the autoloader is called for DataMapperExt, I load the database and DMZ if they’re not loaded yet. That way I can call new Model() wherever I want it and have everything just work without necessarily including the database and DMZ on every page (they’re relatively heavy libraries).
#337 / Jun 14, 2010 3:23pm
I see but in your case, what class do your models extend ? The original DMZ or your custom class ?
#338 / Jun 14, 2010 3:47pm
They extend my custom class (although I also check for DataMapper load in case I happen to have a model that extends DataMapper directly). Since it’s my own simple autoloader, it doesn’t really matter, because I know what needs to be loaded to make it work.
I’m not saying you have to do it this way, of course, but for me, that makes loading my models very convenient.
#339 / Jun 15, 2010 11:09pm
Hello !
I am working with datamapper validation and i am using the function “set_value()” to return the input values when an error is shown, but isn’t working , nothing is returned!
Code:
<input name=“username” value=”<?php echo set_value(‘username’); ?>” />
If i skip the validation with $object->skip_validation()->save() set_value() function works.
Any idea ?
Sorry my english!
#340 / Jun 15, 2010 11:17pm
@Atas
DMZ doesn’t work with the built-in form methods, necessarily. The recommended way to use DMZ with forms it to follow this pattern:
$object = new Object($object_id);
if($this->input->post('save')) {
$object->username = $this->input->post('username');
...
if($object->save()) {
// redirect or whatever
}
}
$this->load->view('form', array('object' => $object));In view:
<input name="username" value="<?php echo $object->username ?>" >This allows you to use the same code to output both the current content and the modified content.
#341 / Jun 15, 2010 11:28pm
Ok, i am goin to try that.
Thanks for your time ! 😊
#342 / Jun 15, 2010 11:30pm
One thing I didn’t include: don’t forget to escape your output (ie: the last bit should have been:
<input name="username" value="<?php echo htmlspecialchars($object->username) ?>" >#343 / Jun 16, 2010 8:06am
First off, just want to say, friggin’ nice work!
Now working with this concept, I feel that my head just can’t get around some parts, or simply put, I’m just incredibly dumb at the moment.
What I’m trying to achieve is grouping up tables nicely to make accessing easy.
I have a table for categories, products and product_images.
I’ve set up the models like this:
class categories extends DataMapper{
public $has_many = array('products');
function __construct($id = NULL)
{
parent::__construct($id);
}
}class products extends DataMapper{
public $has_one = array('category');
public $has_many = array('product_images');
function __construct($id = NULL)
{
parent::__construct($id);
}
}class product_images extends DataMapper{
public $has_one = array('products');
function __construct($id = NULL)
{
parent::__construct($id);
}
}My problem here is… how do I actually access the product_images. I can get to the products… But that’s it. Am I thinking this the wrong way or is there another way to actually achieve my goals?
#344 / Jun 16, 2010 8:49am
@YaZz
First off, you’ve got some naming issues. Your classes need to be singular: category, product, and product_image. (The tables, however, need to be plural.) With the updated inflector helper, DMZ will handle the name conversions. You’ll also need to update your $has_one and $has_many arrays in the same manner.
Then, you access them like this:
$cat = new Category($cat_id); // or find it in a loop, etc.
$products = $cat->product->get();
foreach($products as $product) {
$product->product_image->get();
// now you can loop over the image
}Of course, you can look up a product directly.
Take a look at the example code included with the example application. It was in the full download. There are examples of all of this and more. 😉
#345 / Jun 16, 2010 9:03am
*facepalm* Thanks! Somethings are just too obvious.