Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Allow magic finders to use underscore columns #80

Closed
wants to merge 2 commits into from
Closed

Allow magic finders to use underscore columns #80

wants to merge 2 commits into from

Conversation

Jud
Copy link
Collaborator

@Jud Jud commented Sep 25, 2013

We have an existing application that uses underscores in the database column names. The magic finders don't allow this kind of column naming.

This patch is the first of a few that are required for full support of underscore naming conventions.

::findByProductIdAndOrderId() will now resolve to order_id and product_id if it is defined in the schema. Otherwise, it resolves to orderid and productid.

@lox
Copy link
Owner

lox commented Sep 26, 2013

Another way to approach this might be to just build up an array of aliases based on the defined properties of the underscore version to the non-underscore version. Then you could just do a cheap str_replace on the $method and leave the existing code as it is?

@mtibben
Copy link
Collaborator

mtibben commented Sep 26, 2013

Perhaps there should be some kind of finderNamingStrategy method that could tell the finder how to map magic method names to property names?

@Jud
Copy link
Collaborator Author

Jud commented Sep 26, 2013

@mtibben I thought about that, seems like pheasant should support at least underscore and its current default out of the box.

ActiveRecord provides some ways to override naming conventions:
http://www.slideshare.net/napcs/rails-and-legacy-databases-railsconf-2009

There could be a public static propertyAliases() method, much like properties, that would allow you to return array($new_name => $old_name). Though, that would only really be needed for more exotic db layouts (e.g. 'firstName' => 'person_first_name').

@lox
Copy link
Owner

lox commented Sep 26, 2013

Yeah, interesting. An alternative would be allowing field names to be defined in the properties:

<?php

class Post extends Pheasant\DomainObject
{
  public function properties()
  {
    return array(
        'postid'   => new Types\Integer(11, 'primary auto_increment field=post_id'),
        'title'    => new Types\String(255, 'required'),
        'authorid' => new Types\Integer(11, 'field=author_id')
        );
  }
}

$posts = Post::findByPostIdAndAuthorId(1, 20);
$posts->filter('postid > 5');

It would mean some pretty nasty search and replace in SQL in queries and then lookups in hydration though.

@Jud
Copy link
Collaborator Author

Jud commented Sep 26, 2013

Edge cases of this could be tougher to handle with str_replace.

$entry = Entry::findByOrderProductsIdAndProductsId(111,222);
// When schema is constructed, we build a list of aliases so it looks like
$aliases = array('ProductsId' => 'Products_id')
// in __callStatic $method would be findByOrderProductsIdAndProductsId
echo str_replace(array_keys($aliases), array_values($aliases), $method);
// => findByOrderProducts_idAndProducts_id

Its a contrived example, but sure to be an issue.

Also, to me, this is mostly about method naming. ::findByOrderIdAndProductId just looks better than ::findByOrder_idAndProduct_id (Which makes me cringe). I know it wouldn't be 100% consistent in a way, but I could live with methods being camel case and my properties being in the case of the column.

@lox
Copy link
Owner

lox commented Sep 26, 2013

Yup, I think I agree. Let's just figure out a light-weight way to get underscore support into the magic finder stuff.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants