CodeIgniter Forums
DataMapper ORM v1.8.2 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: DataMapper ORM v1.8.2 (/showthread.php?tid=47243)



DataMapper ORM v1.8.2 - El Forum - 09-25-2012

[eluser]WanWizard[/eluser]
@Andy78,

For starters, get the latest version from Bitbucket. Line 2370 is an empty line in my version, so I haven't got a clue what causes that message.

You get his error if you would do
Code:
$this->$var->method();
and $var is not defined or null. This syntax isn't used in Datamapper.

I assume this will also give that error:
Code:
$value = $this->{$var};
and $var is not defined or null. This is used all over the place.



DataMapper ORM v1.8.2 - El Forum - 09-25-2012

[eluser]rick20[/eluser]
@WanWizard,

I just tried using where_related() but it's actually did LEFT OUTER JOIN with join-table and the child table.
And, the where_related() seems to me always look for 'field' in child table, instead in join-table.

So, from here I think I cannot use where_related().

Actually If I see from the SQL error, when using where_join_field(), I just only need to tell datamapper to use join-table alias name in WHERE condition, instead of the join-table name itself.

Because I see that the LEFT OUTER JOIN already make an alias for the join-table, but the WHERE still access the join-table name. And this outputs the error 'Unknown column ... in WHERE clause'

Any way to do this thing?

Please kindly help.



DataMapper ORM v1.8.2 - El Forum - 09-25-2012

[eluser]WanWizard[/eluser]
Sorry, my bad.

I should have written 'where_join_field' in that example. I'll change it. 'where_related' indeed always operates on the related table.

And it's correct that Datamapper aliases all tables, so when referring to a column in another table you should use the alias, not the table name.

I'm still trying to figure out what you're doing. Can you post the exact query you run, and the output of check_last_query()?


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]rick20[/eluser]
@WanWizard,

Below is my sample codes

Code:
class Model_User extends DataMapper {

  public $table = 'users';

  public $has_many = array(
    'orders' => array(
      'class' => 'model_item',
      'other_field' => 'orders',
      'join_table' => 'orders',
      'join_self_as' => 'user',
      'join_other_as' => 'item'
    ),
    'favorites' => array(
      'class' => 'model_item',
      'other_field' => 'favorites',
      'join_table' => 'favorites',
      'join_self_as' => 'user',
      'join_other_as' => 'item'
    )
  );
  
  function __construct($id = NULL)
  {
    parent::__construct($id);
  }
}

Code:
class Model_Item extends DataMapper {

  public $table = 'items';
  
  public $has_many = array(
    'orders' => array(
      'class' => 'model_user',
      'other_field' => 'orders',
      'join_table' => 'orders',
      'join_self_as' => 'item',
      'join_other_as' => 'user'
    ),
    'favorites' => array(
      'class' => 'model_user',
      'other_field' => 'favorites',
      'join_table' => 'favorites',
      'join_self_as' => 'item',
      'join_other_as' => 'user'
    )
);
  
  function __construct($id = NULL)
  {
    parent::__construct($id);
  }
}

And here are my tables:

Code:
CREATE TABLE IF NOT EXISTS `mc_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(100) NOT NULL,
  `created` datetime NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `mc_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `created` datetime NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `mc_orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  `status` int(11) NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE IF NOT EXISTS `mc_favorites` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `item_id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
)

This is the code in my controller:

Code:
$this->output->enable_profiler(TRUE);
    
$user = new Model_User(1);
$user->orders->where_join_field('orders', 'status', 1)->get();

With this line of code, I got the following error:

Code:
Unknown column 'mc_orders.status' in 'where clause'

SELECT `mc_items`.* FROM (`mc_items`) LEFT OUTER JOIN `mc_orders` orders_orders ON `mc_items`.`id` = `orders_orders`.`item_id` WHERE ( `mc_orders`.`status` = 1 ) AND `orders_orders`.`user_id` = 1

I've tried any combination of codes using where_join_field, include_join_field, where_related, and a lot more I can think of, but still no luck.

Really need your help here, thanks.


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]WanWizard[/eluser]
Thanks.

I have to find time to recreate this to see what goes wrong. I might have to do with the fact that you use advanced relationship definitions, it seems detection of the table alias goes wrong...


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]maikens[/eluser]
I'm trying to do something like this:

Code:
// ...
$model->where_related('relative1/relative2', array('field1' => 'val1', 'field2' => 'val2'))->get();

But it doesn't appear to support having an array for fields instead of a field and value string, as I get a db error with Array as the field name.

Is there a way to perform an advanced get with more than one field and if not, what would be a good alternative?

Thanks,
Matt


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]rick20[/eluser]
@maikens AFAIK the where_related() only accept string for field parameter.
You can find more explanation about where() and its variations from the documentation http://datamapper.wanwizard.eu/pages/getadvanced.html

But in your case, maybe you can use the following:
Code:
$model
  ->where_related('relative1/relative2', 'field1', 'val1')
  ->where_related('relative1/relative2', 'field2', 'val2')
  ->get();

Hope this helps


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]rick20[/eluser]
@WanWizard Thanks so much Smile
I really do wish datamapper keeps better and better everytime.
So far, this is the easiest but powerful ORM for codeigniter I can find from google.

Really appreciate your help Smile


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]maikens[/eluser]
@rick20 thanks. I ended up doing it like this:

Code:
$model->related1->get();
$model->related1->related2->where(array(('field1' => 'val1', 'field2' => 'val2'))->get()

Your way seems a bit cleaner, but meh.

thanks again for the help.


DataMapper ORM v1.8.2 - El Forum - 09-26-2012

[eluser]WanWizard[/eluser]
@rick20,

Doing my best. Problem is a severe lack of time, I've been on 80 hour weeks for quite some time now...