# Contain only when statement is true?

**URL:** <https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938>\
**Category:** Need Help\
**Created:** [April 15, 2019, 12:04pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938 "2019-04-15T12:04:04Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [April 15, 2019, 12:04pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/1 "2019-04-15T12:04:04Z")

</div>

Hii there,

I’m trying to get entries from the `Comments` table in my app.  
Unfortunately, this doesn’t really work out right now as there are some “rules” to loading them than I can not get working by myself.

A the entities have to relevant objects: `reply` and `status`.  
a `reply` is an association to another comment (to which the current comment is a reply) and the `status` is well… the status of the comment (eg. spam, private or public).  
Now what I’m trying to do is selecting a `comment` by id:

```auto
$comment = $this->Comments->findById(15);

```

This is pretty easy, but here comes the tricky part: I want to `contain` the `Reply` but, I want to contain it _only_ if the status is in the `conditions` array.  
the conditions array is built like this:

```auto
$conditions [['status' => 1]];
if($this->user) {
            $conditions[]['user_id'] = $this->user->id;
}
// ... more conditions here

```

So this code should only contain a reply if the reply’s `status` is `1` or if the user is logged in, the `user_id` is the id of the logged in user.  
I wonder how I should write this…

I have already come up with this piece of code, but this throw me an error:

> Integrity constraint violation: 1052 Column ‘status’ in on clause is ambiguous

```auto
$comment = $this->Comments
        ->findById($this->request->getParam('id'))
        ->contain('Reply', function(Query $q) {
          $conditions = [['status' => 1]];

          // Check if the user is logged in
          if($this->user) {
            $conditions[]['user_id'] = $this->user->id;

            // Check if the user may view pending comments
            if($this->user->hasRight('view_pending_comments')) {
              $conditions[]['status'] = 0;
            }
        
            // Check if the user may view spam comments
            if($this->user->hasRight('view_spam_comments')) {
              $conditions[]['status'] = 2;
            }
          }

          return $q->where(['OR' => $conditions]);
        })
        ->first();

```

It results in the following SQL query:

```auto
SELECT Comments.id AS `Comments __id`, Comments.user_id AS `Comments__ user_id`, Comments.reply AS `Comments __reply`, Comments.body AS `Comments__ body`, Comments.posted AS `Comments __posted`, Comments.modified AS `Comments__ modified`, Comments.status AS `Comments __status`, Reply.id AS `Reply__ id`, Reply.user_id AS `Reply __user_id`, Reply.reply AS `Reply__ reply`, Reply.body AS `Reply __body`, Reply.posted AS `Reply__ posted`, Reply.modified AS `Reply __modified`, Reply.status AS `Reply__ status` FROM comments Comments LEFT JOIN comments Reply ON (status = :c0 AND Reply.id = (Comments.reply)) WHERE Comments.id = :c1 LIMIT 1

```

---

<div class="post-metadata">

**Author:** ![dakota](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dakota/32/1421_2.png) [@dakota](https://discourse.cakephp.org/u/dakota)\
**Post date:** [April 15, 2019, 1:49pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/2 "2019-04-15T13:49:25Z")

</div>

> [@FinlayDaG33k](#):
>
> Integrity constraint violation: 1052 Column ‘status’ in on clause is ambiguous

That is the clue here 🙂 You need to give the DB server the full column name since in this case it doesn’t know which `status` column you are referring too. Replace all the column names in your closure function with `$this->Comments->Reply->aliasField('status')` (and `$this->Comments->Reply->aliasField('user_id')`)

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [April 15, 2019, 3:02pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/3 "2019-04-15T15:02:31Z")

</div>

I’m currently not at work so I can’t test it out yet.  
But I think you mean something like this?

```php
function(Query $q) {
  // ... Conditions stuff here
  
  $this->Comments->Reply->aliasField('status');
  $this->Comments->Reply->aliasField('user_id');
  return $q->where(['OR' => $conditions]);
}

```

But wouldn’t this result in it overwriting the objects “permanently” (or atleast until the next request)?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 15, 2019, 3:43pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/4 "2019-04-15T15:43:24Z")

</div>

No, he means like this:

```
$conditions = [[$this->Comments->Reply->aliasField('status') => 1]];

```

Alternately, you could just use

```
$conditions = [['Reply.status' => 1]];

```

though that’s maybe more susceptible to breakage through changes elsewhere in your code.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [April 15, 2019, 4:13pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/5 "2019-04-15T16:13:55Z")

</div>

Thanks, I’ll have a look at it tomorrow 🙂

> [@Zuluru](#):
>
> though that’s maybe more susceptible to breakage through changes elsewhere in your code.

How come?

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [April 15, 2019, 4:34pm UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/6 "2019-04-15T16:34:10Z")

</div>

Well, the former option uses system state to determine the name to use, while the latter is hard-coded. But then again the association chain (`$this->Comments->Reply`) is hard-coded too, so maybe it’s a wash. Couldn’t think of any specific scenarios where one would break and not the other, hence why I said “maybe”. 🙂

I use the `Reply.status` form in my own code, as I find it more friendly for searches to find usage. And lately I’m trying to use the dot notation for all such references, whether or not it’s required to disambiguate, because things change and ambiguity can suddenly appear where there was none before, by adding a new field to a related table for example. A few extra keystrokes seems a small price to pay for the additional peace of mind.

---

<div class="post-metadata">

**Author:** ![FinlayDaG33k](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/finlaydag33k/32/1049_2.png) [@FinlayDaG33k](https://discourse.cakephp.org/u/FinlayDaG33k)\
**Post date:** [April 16, 2019, 7:58am UTC](https://discourse.cakephp.org/t/contain-only-when-statement-is-true/5938/7 "2019-04-16T07:58:00Z")

</div>

> [@Zuluru](#):
>
> $this-\>Comments-\>Reply-\>aliasField(‘status’)

Tested it, seems to work 🙂
