EllisLab text mark
Advanced Search
53 of 63
53
   
DataMapper ORM v1.8.2
Posted: 10 October 2012 06:26 AM   [ Ignore ]   [ # 781 ]   [ Rating: 0 ]
Joined: 2012-10-09
6 posts
WanWizard - 09 October 2012 11:56 AM

If you want both the limited result and an unlimited count, there is no way you can do that with a single query (unless you fetch everything and apply the limit in code, but that doesn’t give you any gain).

There is however no need to run the get() twice.

Build your complete query, then run

$count $venue->count(); 

instead of a get.

after that, stick your limit and offset on, and run the get.

I can’t use the count() function, as I am using a custom select() in order to use my ‘having’ clause - the count() function overrides my select, to COUNT(*) and I get an unknown column ‘distance’ error as I am no longer selecting “<my math> as distance”.

Is there any way I can use the count() function without having to build my ‘select’ portion twice? Just for the sake of cleaner code.

Thanks

 
Posted: 10 October 2012 07:58 AM   [ Ignore ]   [ # 782 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

Build your query to the point you need, then clone the object, and run the count on the clone?

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 10 October 2012 08:37 AM   [ Ignore ]   [ # 783 ]   [ Rating: 0 ]
Joined: 2012-10-09
6 posts
WanWizard - 10 October 2012 07:58 AM

Build your query to the point you need, then clone the object, and run the count on the clone?

Ah, I was trying to clone it by doing:

$clone = clone $venue 

But then spotted in the DataMapper docs the get_clone() function!

Thanks for your help, DataMapper is fantastic by the way!

 
Posted: 10 October 2012 11:10 AM   [ Ignore ]   [ # 784 ]   [ Rating: 0 ]
Joined: 2012-10-09
6 posts

Sorry to be a pain, but I suck at SQL so can’t even get my head around the theory of what I’m trying to do this time..

This is similar to my venue search, but this time it’s an ‘event’ search. Each event ‘has one’ venue, and I want to filter events by venues that within 5 miles of the location entered.

The code below works, except it overrides the values in my $event instance. For example, both events and venues have a field ‘url_segment’, the code below overrides the event url segment with the venue one, and I can’t access the venue by doing $event->venue->name, I have to do $event->name.

What am I doing wrong here?

$event = new Event();  
  
$result['status''success';
  
  if (
$location AND $location != 'any' AND strlen($location) >= 3)
  
{
   $l 
= new Location();
   
$l->where('name'$location)->get();
   
   
// Either get existing lat/lon if exists, or encode new one
   
$location $l->exists() ? $l->where('name'$location)->get(1) : $this->add_gmap_json($location);
   
   if (isset(
$location->lat))
   
{
    $venue 
= new Venue();
    
$event->select_related($venue'*, (((acos(sin(('.$location->lat.' *pi()/180))*sin((`lat`*pi()/180))+cos(('.$location->lat.'*pi()/180))*cos((`lat`*pi()/180))*cos((('.$location->lon.'-`lon`)*pi()/180))))*180/pi())*60*1.1515) as distance');
    
$event->having('distance < 5');
    
$result['loc'$location;
   
}
   
else
   
{
    $result[
'status''error';
   
}
  }
  
  
/* more filtering */
  
  
$clone $event->get_clone();
  
$clone->get();
  
  
$result['num_rows'$clone->result_count();
    
  if (
$limit OR $offset)
  
{
   $event
->limit($limit);
   
$event->offset($offset);
  
}
  
  $result[
'results'$event->get();

  return 
$result

Thanks again for your help!

 
Posted: 10 October 2012 11:22 AM   [ Ignore ]   [ # 785 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

Try to rewrite it using include_related().

It allows you to prefix the column names of the joined table (by default with the model name) to avoid column name collisions…

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 10 October 2012 11:34 AM   [ Ignore ]   [ # 786 ]   [ Rating: 0 ]
Joined: 2012-10-09
6 posts
WanWizard - 10 October 2012 11:22 AM

Try to rewrite it using include_related().

It allows you to prefix the column names of the joined table (by default with the model name) to avoid column name collisions…

Gah! It was staring me right in the face the whole time, I’d been getting sql errors because my select statement included a second *!

You rock, thanks

 
Posted: 24 October 2012 02:41 AM   [ Ignore ]   [ # 787 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

Greetings,

I am not sure if this should be possible, but I am noticing I am building around datamapper to get this to work.

I have no idea what it is called but: Lets say you have a relation with a class that can be one of many classes. Example: You got a Bowl with a relationship ‘contents’ and the contents can be all kinds of objects, Apple, Chocolate, Bananas, etc.

I feel it is close to datamapper, but that the database itself is not really playing nice with the concept. Am considering a huge jointable so I at least only need one table, but is there any tricks to get something like that to work in Datamapper without custom functions?

 Signature 

- Simon

 
Posted: 24 October 2012 02:51 AM   [ Ignore ]   [ # 788 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

No, this is not possible.

Datamapper (most ORM’s do) has a fixed one-to-one object mapping in it’s relations. This would require a combination of a FK and an object name in the parent table, which would then be used to determine which relation to access. It would also mean querying all these relations, and unifying the result of the query, which can get tricky if these different objects have different properties.

So, it would involve a new type of relation, that defines all underlying models (instead of just one), extension to the get() methods to query them all instead of just one, and new hydration logic to translate the results back to the object.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 24 October 2012 02:55 AM   [ Ignore ]   [ # 789 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

That’s what I thought, but it never hurts to ask.

I can get around it regardless. I can see the Bowl example to be a perfectly workable design decision thus I wondered.

Regardless thank you very much for Datamapper still, it has found its way into every new project. smile

 Signature 

- Simon

 
Posted: 25 October 2012 04:33 PM   [ Ignore ]   [ # 790 ]   [ Rating: 0 ]
Joined: 2012-03-28
71 posts

Am I able to define my own name for a joining field instead of joinTableName_id?

I want to be able to name a field ‘image’ that points to the id of a ‘files’ table.

 
Posted: 25 October 2012 05:05 PM   [ Ignore ]   [ # 791 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

No, all foreign keys are required to have the suffix ‘_id’.

It’s hardcoded, and very complex to change (which is why it hasn’t happened yet).

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 26 October 2012 05:26 AM   [ Ignore ]   [ # 792 ]   [ Rating: 0 ]
Avatar
Joined: 2009-06-12
420 posts

But it might in 2.0!... Right? smile

 Signature 

- Simon

 
Posted: 26 October 2012 07:03 AM   [ Ignore ]   [ # 793 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

It’s at the top of the todo list, yes.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
Posted: 03 November 2012 10:18 PM   [ Ignore ]   [ # 794 ]   [ Rating: 0 ]
Joined: 2011-05-18
15 posts

Humm i´m new at Datamapper ORM, and im just reading all the documentation and trying some snnipets… But now this come to my mind…. If your are no longer mantaining HTML Form, how should i generate forms based on the models… i dont get it very well.

Thank you in advanced

 
Posted: 04 November 2012 06:53 AM   [ Ignore ]   [ # 795 ]   [ Rating: 0 ]
Avatar
Joined: 2008-11-04
4404 posts

Easy answer is: like you would make a form otherwise.

Having said that, there’s nothing wrong with the form extension, it works fine, check the included example application.

 Signature 

Me: WanWizard.eu | My company: Exite | Datamapper: DataMapper ORM

 
53 of 63
53