CodeIgniter Forums
DataMapper ORM v1.8.0 - 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.0 (/showthread.php?tid=37531)



DataMapper ORM v1.8.0 - El Forum - 03-02-2011

[eluser]endielo[/eluser]
Dear All,

I have a problem when I using the to_json() plug-in with include_related(), I want to output all the object fields and also, the include related fields.


Code:
$object->include_related('user', 'name');
$object->get();

$object->to_json();

Is there any other way but not passing a array of field to to_json() to specify which fields need to output.


Thank You
--
Endie Lo


DataMapper ORM v1.8.0 - El Forum - 03-02-2011

[eluser]WanWizard[/eluser]
No, there is no way to automatically determine which fields should be included, $this->fields only contains the columns of the object itself.

You could try to make your own version of to_json which simply iterates over the results in the object.


DataMapper ORM v1.8.0 - El Forum - 03-02-2011

[eluser]endielo[/eluser]
[quote author="WanWizard" date="1299088307"]No, there is no way to automatically determine which fields should be included, $this->fields only contains the columns of the object itself.

You could try to make your own version of to_json which simply iterates over the results in the object.[/quote]

Thank to WanWizard,

I have modify the datamapper class and make my own version to_json finally.
But not sure will it introduce bugs. hopefully not :down:

--
Endie Lo


DataMapper ORM v1.8.0 - El Forum - 03-02-2011

[eluser]oooobs[/eluser]
Hello

I am using CI2.0 with PHP 5.3

I just started to use "Datamapper ORM" and it is excellent!! however their is a one big problem regarding the classes' names

I have a database table called "users" so my dm model is "user" and also I have a controller with the same name "user"?

so using the "user" model within the "user" controller is imposible!!

what is the best way to solve this problem?

Many Thanks

best regards


DataMapper ORM v1.8.0 - El Forum - 03-02-2011

[eluser]endielo[/eluser]
[quote author="oooobs" date="1299147857"]Hello

I am using CI2.0 with PHP 5.3

I just started to use "Datamapper ORM" and it is excellent!! however their is a one big problem regarding the classes' names

I have a database table called "users" so my dm model is "user" and also I have a controller with the same name "user"?

so using the "user" model within the "user" controller is imposible!!

what is the best way to solve this problem?

Many Thanks

best regards[/quote]

In general for me,

Controller should be 'users'
datamapper model should be 'user'
DB table name should be 'users'

ref to documentation
you can also define your model like :
Code:
class Country extends DataMapper {

    var $table = 'countries'; // which should be the name of your table.

    function __construct($id = NULL)
    {
        parent::__construct($id);
    }
}

hope can help you
--
Endie Lo


DataMapper ORM v1.8.0 - El Forum - 03-04-2011

[eluser]brainer[/eluser]
Datamapper, seems to be really cool. Looking forward to getting to terms with it!

However I am stuck on something at the moment and it's pretty frustrating! Sad

Let's just say I have these tables:

Code:
books
- id
- name
Code:
pages
- id
- book_id
- number

Now, lets just say I want to make a list of books, however I don't want to list any books that don't have pages. How would I do that with DataMapper? In regular SQL I know I could just do:

Code:
SELECT * FROM books
INNER JOIN pages
ON books.id = pages.book_id

but is there some special way of doing it with DataMapper, so I don't need to write raw SQL?


DataMapper ORM v1.8.0 - El Forum - 03-05-2011

[eluser]IgnitedCoder[/eluser]
Excellent work on DM... I've got my models defined but I need to include the fields from a related table, for example:
Code:
Class User extends DataMapper{

function __constructor($id=0){
  parent::__constructor($id);
}

var $has_many = array('relateduser' => array('class' => 'user','other_field' => 'user'),
                          'user' => array('other_field' => 'relateduser'));


function getRelated(){
return $this->relateduser->get();
}

}

This works very well to get the related users, but I also need to include the other fields from relatedusers_users table. For example there a relation type field.

Table [relatedusers_users]

[id] [user_id] [relateduser_id] [type]
1 4 1 sales
2 1 4 contact
3 2 4 doctor

how would I get the related type field... I don't want to loop over the records to simply retrieve the type field.

Thanks,
Brendan


DataMapper ORM v1.8.0 - El Forum - 03-05-2011

[eluser]brainer[/eluser]
@BrendanRehman

I think you might be looking for include_related() or include_join_fields(). Have a look, here: http://datamapper.wanwizard.eu/pages/getadvanced.html


DataMapper ORM v1.8.0 - El Forum - 03-05-2011

[eluser]endielo[/eluser]
@Brainer,

do you mind writing the code like this:
Code:
$book->include_related_count('page')->having('page_count >' , 0)->get();

the sql generated by Datamapper will like this:
Quote:SELECT `books`.*, (SELECT COUNT(*) AS count
FROM (`pages`)
LEFT OUTER JOIN `books` `books_subquery` ON `books_subquery`.`id` = `pages`.`book_id`
WHERE `books_subquery`.id = `books`.`id`) AS page_count
FROM (`books`)
HAVING `page_count` >0

I'm not sure will this slow down the performance because there have a subquery,
however, when someone ask me to get the list which have only 1 page. i can still use the above code

P.S. I'm also a newbie in CI/ORM/Datamapper. and Sorry for my poor english.


DataMapper ORM v1.8.0 - El Forum - 03-05-2011

[eluser]WanWizard[/eluser]
What exactly are you trying to achieve with this query? It looks quite complicated to me, for the result it produces...