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 - 05-28-2011

[eluser]Ryuuzaki92[/eluser]
Hey guys, sorry for interrupting, but I'm trying to figure out how to check if the user favourites a bookmark. The following query is what I'm trying to achieve but I'm having a hard time trying to port it to DataMapper.

Code:
SELECT
    bookmarks.*,
    ( ! ISNULL (user_favourites.bookmark_id) AND ! ISNULL (bookmarks.user_id)) AS is_favourite
FROM
    bookmarks
LEFT JOIN
    user_favourites
ON
    bookmarks.id = user_favourites.bookmarks_id
AND
    user_favourites.user_id = 1

Basically, the user_favourites table has two fields: user_id and bookmark_id.

I know I could use raw queries but I would like to use the pagination built into the library.

Thanks.


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

[eluser]Anestetikas[/eluser]
Delete this..


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

[eluser]Arministrator[/eluser]
I've been scratching my head about something that seems really simple:

I need to retrieve just my latest entry (last donation) from the table. Usually I'd use active record's row() after I select and order data by some parameter, to get a single result row.

How should I go about it within DataMappers functions? I'd like to try to use only DataMappers functions for all the db related logic, and not mix it with active record. Should I use $object->func and SQL? I feel there's a simple way to this.

For a bit more background (probably irrelevant), I need to get my latest donation's created time, and I plan to do a timespan() on it so I can see if certain amount of time passed. My dates are UNIX timestamps.


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

[eluser]WanWizard[/eluser]
Could you post the database schema for these tree tables, so I can runs some tests?

=edit= question was deleted.


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

[eluser]WanWizard[/eluser]
@Arministrator,

The latest entry using a query ( which would then need the correct order_by() and a limit(1) ) or from a given resultset ( which you retrieve with end($object->all) )?


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

[eluser]Arministrator[/eluser]
Right, I needed to mention that. It's from a result set.

I get all related donations for a donor by $donor->donation->order_by('something', 'desc')->get_iterated(); then loop through, so I figure it would be best to use the result which is already retrieved, compared to querying the same thing again.

EDIT: I guess writing a new query, something like $donor->donation->order_by('created', 'desc')->limit(1)->get(); wouldn't be the worst idea?

Although a more optimized piece of code would be extracting the data from an already retrieved row. I'm just writing all that comes to mind right now, sorry if I'm spammingSmile

EDIT again: Hm, when I use orderby and limit, it overwrites the $donor->donation result I'm using for a foreach loop. Quick fix comes to mind, to query directly from a view, but it's just bad practice.


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

[eluser]WanWizard[/eluser]
If you use get_interated(), there is no result set, it is fetched when you loop over the object. If you want access to the "all" array that contains the result set, you need to use get() instead.

If you need the last one after the loop, the loop variable contains the last object retrieved.
Code:
foreach ( $object as $row )
{
    // do something here
}
// contains the last row
var_dump( $row );

If you need it in (or before) the loop, this doesn't work. If the result set is small, I would opt for get() instead. If it's large, use a second query.


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

[eluser]Arministrator[/eluser]
OK, I'll try both the get and the second query method and see what fits better. Thanks again.


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

[eluser]Mark Price[/eluser]
I cannot figure out how to set a value as null in the database. I have tried doing doing something like:

Code:
$u = new User();
$u->username = "foo";
$u->password = "bar";
$u->email = NULL;
$u->save();

But this does not seem to work. Please help.


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

[eluser]IgnitedCoder[/eluser]
Set the allow null for the email field in the db.