We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Relationships, conditions and backslashes

Hello,

I am trying to pull a user's thread from the database using the relationship $user->thread but I am running into problems when adding conditions to the relationship. Specifically, I am testing for the following condition "model = App\Common\Models\Users" but it always returns an empty result set. I am guessing it has to do with backslash escaping. I have tried variations like "model = App\\Common\\Models\\Users" but couldn't get it to work. The only variation I was able to use successfully was "model LIKE %Users".

Note: single quotes were removed because the forum is encoding them as html entities...

Threads

id model_id model
1 1 App\Common\Models\Users
2 1 App\Common\Models\DontSelectMe

In this scenario, I want to pull the first row but not the second row.

Users

id username
1 john
// App\Common\Models\Users.php

// Threads relationship
$this->hasOne(
  "id",
  "App\Common\Models\Threads",
  "model_id",
  array(
    "params" => array(
      "conditions" => "model = 'App\Common\Models\Users'",
    ),
    "alias" => "Thread"
  )
);

Thank you for your time!



955
Accepted
answer
edited Apr '16

I tried triple slashes and it ended up being the solution:

// App\Common\Models\Users.php

// Threads relationship
$this->hasOne(
  "id",
  "App\Common\Models\Threads",
  "model_id",
  array(
    "params" => array(
      "conditions" => "model = 'App\\\Common\\\Models\\\Users'",
    ),
    "alias" => "Thread"
  )
);