query->condition() with namespaced classes fails #87

Closed
meglio opened this Issue Sep 3, 2011 · 18 comments

8 participants

@meglio

Hello.

Here is the code that thrown a PropelException:

$userQuery = new \core\orm\UserQuery();
$userQuery->condition('email', 'User.Email = ?', $emailOrNick);

Please look at exception stack:
Exception Stack

Here is table definition in schema.xml:

<table name="users" phpName="User">
        <column name="id" type="BIGINT" required="true" primaryKey="true" autoIncrement="true"/>
        <column name="notary_id" type="BIGINT" required="true"/>
        <column name="notary_admin" type="BOOLEAN" required="true" defaultValue="0"/>
        <column name="email" type="VARCHAR" size="180" required="false" description="Email is unique but not required for notary helpers. However it is strictly required for notary owners." />
        <column name="nick" type="VARCHAR" size="180" required="true" description="If only email specified, nickname will be set to email. This rule works for both notary owners and helpers."/>
        <column name="pass" type="LONGVARCHAR" required="true" />
        <column name="date_created" type="TIMESTAMP" required="true" defaultExpr="CURRENT_TIMESTAMP"/>
        <column name="date_authorized" type="TIMESTAMP" required="true" defaultExpr="CURRENT_TIMESTAMP"/>
        <column name="date_visited" type="TIMESTAMP" required="true" defaultExpr="CURRENT_TIMESTAMP"/>
        <column name="auth_token" type="VARCHAR" size="40" required="false" description="Authorization token - sha1 of some random key."/>
        <column name="active" type="BOOLEAN" defaultValue="1" description="Notary superuser can disable access for notary helper."/>
        <index name="ind_notary_ids">
            <index-column name="notary_id"/>
            <index-column name="notary_admin"/>
        </index>
        <unique name="uniq_user_emails">
            <unique-column name="email"/>
        </unique>
        <unique name="uniq_user_nicks">
            <unique-column name="nick"/>
        </unique>
        <foreign-key name="frn_notary_of_user" foreignTable="notaries" onDelete="cascade" onUpdate="cascade">
            <reference local="notary_id" foreign="id" />
        </foreign-key>
        <index name="ind_user_created">
            <index-column name="date_created"/>
        </index>
        <index name="ind_user_authorized">
            <index-column name="date_authorized"/>
        </index>
        <index name="ind_user_visited">
            <index-column name="date_visited"/>
        </index>
        <index name="ind_user_passwords">
            <index-column name="pass"/>
        </index>
        <unique name="uniq_user_auth_tokens">
            <unique-column name="auth_token"/>
        </unique>
    </table>

I have also tried following but nothing worked:

$userQuery->condition('email', 'user.email = ?', $emailOrNick);
$userQuery->condition('email', 'user.Email = ?', $emailOrNick);
$userQuery->condition('email', 'User.email = ?', $emailOrNick);
$userQuery->condition('email', 'User.Email = ?', $emailOrNick);
$userQuery->condition('email', '\core\orm\User.Email = ?', $emailOrNick);
@willdurand
Propel member

Hi,

To set a model alias will do the job.

$userQuery = new \core\orm\UserQuery();
$userQuery->setModelAlias('q');
$userQuery->condition('email', 'q.Email = ?', $emailOrNick);
@willdurand willdurand closed this Sep 12, 2011
@meglio

Thanks, this worked. Can you explain another 2 things, please.

  1. Why must we use aliases?
  2. How to alias joined tables?
@willdurand
Propel member
  1. Because it solved your problem ;)
  2. In the join() method ?
@meglio
  1. Can you please explain why it does not work without aliases, what is the reason?
  2. Yes, with join() - what if my where() will mention joined table, how must I alias it?

Thanks,
Anton

@kesoil

I think this is the bug I reported in the old bug tracker: http://trac.propelorm.org/ticket/1485

@meglio

kesoil, yes really it looks like a bug, I see now.
willdurand, can you confirm it is bug and it will be fixed in future versions?
Thanks.

@willdurand
Propel member
@fzaninotto fzaninotto reopened this Oct 30, 2011
@fzaninotto
Propel member

Since it's a bug, let's reopen the Issue. @willdurand, can you take a look?

@willdurand
Propel member

Sure, but I thought it was solved..

@Xosofox

It's not... as I mentioned on the dev-maillist, the ModelCriteria tries to find the matching Class by comparing it with the retrun value of getModelAliasOrName.
As far as I can see, this function will receive the full namespaced class name. And of course, \Vendor\MyBundle\MyClass != MyClass, so it cannot find anything

@jaugustin
Propel member

of course it's a bug, but we fixed it in Propel 2 ;)

I will try to backport code from Propel2 and try to fix this issue (my collegue got it today ;) )

@jaugustin
Propel member

this seems to be fixed in the latest propel version (I will tel my colleague to upgrade their propel 1.6.4 to last version) ;)

@willdurand
Propel member

@jaugustin is it ok?

@jaugustin
Propel member

From my tests I say yes but my dev didn't take time to upgrade their propel version.

I think you could close the issue and if it's still broken I will reopen it ;)

@willdurand
Propel member

roger

@willdurand willdurand closed this Apr 18, 2012
@eymeric

Hi,

I got the same issue on 1.6.5 version

$query = ResourceQuery::create('alias')
            ->where("alias.Label LIKE ?",$string_query)

// Works but

    $query = ResourceQuery::create()
            ->where("Resource.Label LIKE ?",$string_query)

//Doesnt

Is it solved ?

@matthewpeach

Same problem in 1.6.6.

$q = \Path\To\CategoryQuery::create();
$q->where('Category.Id = ?', 1);
$q->find();
PropelException: Cannot determine the column to bind to the parameter in clause 'Category.Id = ?'
in /var/www/workspaces/matt/mightyape/vendor/propel/propel1/runtime/lib/query/ModelCriteria.php
on line 1819
@jaugustin
Propel member

we should first add tests in ModelCriteriaWithNamespaceTest to prove the issue and then fix it :
https://github.com/propelorm/Propel/blob/master/test/testsuite/runtime/query/ModelCriteriaWithNamespaceTest.php#L23

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment