<p>I just found the answer here: <a href="https://craftcms.stackexchange.com/questions/30900/user-query-in-php-does-not-work">User query in PHP does not work</a></p>
<p>I have to add &quot;anyStatus()&quot; to my query:</p>
<pre><code>    $comment = Entry::find()
        -&gt;section('articleComments')
        -&gt;id($commentId)
        -&gt;anyStatus()
        -&gt;one();
</code></pre>
<p>Thanks for the nudge in the right direction <a href="https://craftcms.stackexchange.com/users/6886/matthias-redl-mann">Matthias Redl-Mann</a></p>

CC BY-SA 4.0
2024-06-03T15:04:56.950Z
2024-06-03T15:04:56.950Z
smaniren
<p>I don't get, how i should query for an entry by ID in Craft CMS 5.</p>
<p>I have an entry in my &quot;Article Comments&quot; section:</p>
<p>The Section contains the entry type &quot;Comments Content&quot;. The Entry Type contains the following fields:</p>
<ul>
<li>Email</li>
<li>Comment</li>
<li>Parent Comment</li>
<li>Article</li>
<li>Verification Token</li>
</ul>
<p>After submitting a form, an article comment gets created and an email containing the link to verify the email is sent.</p>
<p>The link contains the following information:</p>
<ul>
<li>id of the comment</li>
<li>token of the comment</li>
</ul>
<p>for example: <a href="https://test.ddev.site/comments/verify?id=729&amp;token=yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx" rel="nofollow noreferrer">https://test.ddev.site/comments/verify?id=729&amp;token=yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx</a></p>
<p>I built the following controller to verify the link:</p>
<pre><code>
public function actionVerify(): Response
{
    Craft::info('actionVerify called', __METHOD__);

    $request = Craft::$app-&gt;getRequest();
    $commentId = $request-&gt;getQueryParam('id');
    $token = $request-&gt;getQueryParam('token');

    Craft::info('Received GET data: ' . print_r([
        'commentId' =&gt; $commentId,
        'token' =&gt; $token,
    ], true), __METHOD__);

    Craft::info('Received comment ID: ' . $commentId, __METHOD__);

    // QUERY:
    $comment = Entry::find()
        -&gt;section('articleComments')
        -&gt;id($commentId)
        -&gt;one();

    Craft::info('Query result: ' . print_r($comment, true), __METHOD__);

    if ($comment) {
        Craft::info('Comment found: ' . print_r($comment-&gt;toArray(), true), __METHOD__);
    } else {
        Craft::error('Comment not found for ID: ' . $commentId, __METHOD__);
        return $this-&gt;asErrorJson('Invalid verification link. Comment not found.');
    }
}

</code></pre>
<p>this is the corresponding log:</p>
<pre><code>2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] actionVerify called {&quot;memory&quot;:1446312} 
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received GET data: Array
(
    [commentId] =&gt; 729
    [token] =&gt; yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx
)
 {&quot;memory&quot;:1446656} 
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received comment ID: 729 {&quot;memory&quot;:1446928} 
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Query result:  {&quot;memory&quot;:1680792} 
2024-06-02 13:50:25 [web.ERROR] [modules\CommentsModule\controllers\CommentsController::actionVerify] Comment not found for ID: 729 {&quot;memory&quot;:1681064} 

</code></pre>
<p>so, the comment is not found.</p>
<p>interestingly, i can find the comment, when i'm querying like this:</p>
<pre><code>    // QUERY:
    $comment = Entry::find()
        -&gt;section('articleComments')
        -&gt;id($commentId)
        -&gt;limit(1);
</code></pre>
<p>log:</p>
<pre><code>2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] actionVerify called {&quot;memory&quot;:1446312} 
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received GET data: Array
(
    [commentId] =&gt; 729
    [token] =&gt; yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx
)
 {&quot;memory&quot;:1446656} 
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received comment ID: 729 {&quot;memory&quot;:1446928} 
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Query result: craft\elements\db\EntryQuery Object
(
    [_events:yii\base\Component:private] =&gt; Array
        (
        )

    [_eventWildcards:yii\base\Component:private] =&gt; Array
        (
        )

    [_behaviors:yii\base\Component:private] =&gt; Array
        ( 

....AND SO ON....
</code></pre>
<p>When I now try to update my comment, i get the following error message:</p>
<p>code:</p>
<pre><code>public function actionVerify(): Response
{
    Craft::info('actionVerify called', __METHOD__);

    $request = Craft::$app-&gt;getRequest();
    $commentId = $request-&gt;getQueryParam('id');
    $token = $request-&gt;getQueryParam('token');

    Craft::info('Received GET data: ' . print_r([
        'commentId' =&gt; $commentId,
        'token' =&gt; $token,
    ], true), __METHOD__);

    Craft::info('Received comment ID: ' . $commentId, __METHOD__);

    // QUERY:
    $comment = Entry::find()
        -&gt;section('articleComments')
        -&gt;verificationToken($token)
        -&gt;limit(1);

    Craft::info('Query result: ' . print_r($comment, true), __METHOD__);

    if ($comment) {
        Craft::info('Comment found: ' . print_r($comment-&gt;toArray(), true), __METHOD__);
        $comment-&gt;setFieldValue('verificationToken', null); // Clear the token
        $comment-&gt;enabled = true; // Enable the comment
    } else {
        Craft::error('Comment not found for ID: ' . $commentId, __METHOD__);
        return $this-&gt;asErrorJson('Invalid verification link. Comment not found.');
    }
}

</code></pre>
<p>error:</p>
<pre><code>Unknown Method – yii\base\UnknownMethodException
Calling unknown method: craft\elements\db\EntryQuery::setFieldValue()
1. in /var/www/html/vendor/yiisoft/yii2/base/Component.phpat line 300
</code></pre>
<p>I really dont get what i'm doing wrong here. Can someone help?</p>
<p><strong>EDIT 03.06.2024:</strong></p>
<p>The error message above makes sense as i'm trying to use a entry method (setFieldValue) on a entryQuery...</p>
<p>The Entry exists in the db:</p>
<p>SELECT * FROM entries WHERE ID = 729:</p>
<pre><code>+-----+-----------+----------+----------------+---------+--------+---------------------+------------+----------------------+---------------------+---------------------+
| id  | sectionId | parentId | primaryOwnerId | fieldId | typeId | postDate            | expiryDate | deletedWithEntryType | dateCreated         | dateUpdated         |
+-----+-----------+----------+----------------+---------+--------+---------------------+------------+----------------------+---------------------+---------------------+
| 729 |         4 |     NULL |           NULL |    NULL |     24 | 2024-06-02 19:14:00 | NULL       |                 NULL | 2024-06-02 19:14:36 | 2024-06-02 19:14:36 |
+-----+-----------+----------+----------------+---------+--------+---------------------+------------+----------------------+---------------------+---------------------+
1 row in set (0.00 sec)

</code></pre>
<p>but somehow I cannot query it through my controller. Any ideas?</p>

CC BY-SA 4.0
2024-06-03T10:24:03.110Z
What is the status of your comments? Are you using multi sites? Try playing around with the 2 properties in your query.
Matthias Redl-Mann
CC BY-SA 4.0
2024-06-03T14:42:03.817Z
i'm not setting the state when generating the comments. they are disabled when i check them in the cms. when i select the entry through sql i find the article...
SELECT * FROM entries WHERE ID = 729;
smaniren
<query><controller><craft5>
Not able to query by id
# Not able to query by id

(Asked by: username_0 on 2024-06-02)

I don't get, how i should query for an entry by ID in Craft CMS 5.
I have an entry in my "Article Comments" section:
The Section contains the entry type "Comments Content". The Entry Type contains the following fields:

Email
Comment
Parent Comment
Article
Verification Token

After submitting a form, an article comment gets created and an email containing the link to verify the email is sent.
The link contains the following information:

id of the comment
token of the comment

for example: https://test.ddev.site/comments/verify?id=729&token=yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx
I built the following controller to verify the link:

public function actionVerify(): Response
{
    Craft::info('actionVerify called', __METHOD__);

    $request = Craft::$app->getRequest();
    $commentId = $request->getQueryParam('id');
    $token = $request->getQueryParam('token');

    Craft::info('Received GET data: ' . print_r([
        'commentId' => $commentId,
        'token' => $token,
    ], true), __METHOD__);

    Craft::info('Received comment ID: ' . $commentId, __METHOD__);

    // QUERY:
    $comment = Entry::find()
        ->section('articleComments')
        ->id($commentId)
        ->one();

    Craft::info('Query result: ' . print_r($comment, true), __METHOD__);

    if ($comment) {
        Craft::info('Comment found: ' . print_r($comment->toArray(), true), __METHOD__);
    } else {
        Craft::error('Comment not found for ID: ' . $commentId, __METHOD__);
        return $this->asErrorJson('Invalid verification link. Comment not found.');
    }
}


this is the corresponding log:
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] actionVerify called {"memory":1446312} 
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received GET data: Array
(
    [commentId] => 729
    [token] => yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx
)
 {"memory":1446656} 
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received comment ID: 729 {"memory":1446928} 
2024-06-02 13:50:25 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Query result:  {"memory":1680792} 
2024-06-02 13:50:25 [web.ERROR] [modules\CommentsModule\controllers\CommentsController::actionVerify] Comment not found for ID: 729 {"memory":1681064} 


so, the comment is not found.
interestingly, i can find the comment, when i'm querying like this:
    // QUERY:
    $comment = Entry::find()
        ->section('articleComments')
        ->id($commentId)
        ->limit(1);

log:
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] actionVerify called {"memory":1446312} 
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received GET data: Array
(
    [commentId] => 729
    [token] => yJt66cu_bgKhMZ4BGOeFIweAe0nk_Hcx
)
 {"memory":1446656} 
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Received comment ID: 729 {"memory":1446928} 
2024-06-02 13:53:05 [web.INFO] [modules\CommentsModule\controllers\CommentsController::actionVerify] Query result: craft\elements\db\EntryQuery Object
(
    [_events:yii\base\Component:private] => Array
        (
        )

    [_eventWildcards:yii\base\Component:private] => Array
        (
        )

    [_behaviors:yii\base\Component:private] => Array
        ( 

....AND SO ON....

When I now try to update my comment, i get the following error message:
code:
public function actionVerify(): Response
{
    Craft::info('actionVerify called', __METHOD__);

    $request = Craft::$app->getRequest();
    $commentId = $request->getQueryParam('id');
    $token = $request->getQueryParam('token');

    Craft::info('Received GET data: ' . print_r([
        'commentId' => $commentId,
        'token' => $token,
    ], true), __METHOD__);

    Craft::info('Received comment ID: ' . $commentId, __METHOD__);

    // QUERY:
    $comment = Entry::find()
        ->section('articleComments')
        ->verificationToken($token)
        ->limit(1);

    Craft::info('Query result: ' . print_r($comment, true), __METHOD__);

    if ($comment) {
        Craft::info('Comment found: ' . print_r($comment->toArray(), true), __METHOD__);
        $comment->setFieldValue('verificationToken', null); // Clear the token
        $comment->enabled = true; // Enable the comment
    } else {
        Craft::error('Comment not found for ID: ' . $commentId, __METHOD__);
        return $this->asErrorJson('Invalid verification link. Comment not found.');
    }
}


error:
Unknown Method – yii\base\UnknownMethodException
Calling unknown method: craft\elements\db\EntryQuery::setFieldValue()
1. in /var/www/html/vendor/yiisoft/yii2/base/Component.phpat line 300

I really dont get what i'm doing wrong here. Can someone help?
EDIT 03.06.2024:
The error message above makes sense as i'm trying to use a entry method (setFieldValue) on a entryQuery...
The Entry exists in the db:
SELECT * FROM entries WHERE ID = 729:
+-----+-----------+----------+----------------+---------+--------+---------------------+------------+----------------------+---------------------+---------------------+
| id  | sectionId | parentId | primaryOwnerId | fieldId | typeId | postDate            | expiryDate | deletedWithEntryType | dateCreated         | dateUpdated         |
+-----+-----------+----------+----------------+---------+--------+---------------------+------------+----------------------+---------------------+---------------------+
| 729 |         4 |     NULL |           NULL |    NULL |     24 | 2024-06-02 19:14:00 | NULL       |                 NULL | 2024-06-02 19:14:36 | 2024-06-02 19:14:36 |
+-----+-----------+----------+----------------+---------+--------+---------------------+------------+----------------------+---------------------+---------------------+
1 row in set (0.00 sec)


but somehow I cannot query it through my controller. Any ideas?

**Tags:** query, controller, craft5

> **Comments:**
> **username_1**: What is the status of your comments? Are you using multi sites? Try playing around with the 2 properties in your query.  
> **username_0**: i'm not setting the state when generating the comments. they are disabled when i check them in the cms. when i select the entry through sql i find the article... SELECT * FROM entries WHERE ID = 729;  

---

## 1 answers

### Answer by username_0 on 2024-06-03. Score: 2 | Accepted answer

I just found the answer here: User query in PHP does not work
I have to add "anyStatus()" to my query:
    $comment = Entry::find()
        ->section('articleComments')
        ->id($commentId)
        ->anyStatus()
        ->one();

Thanks for the nudge in the right direction username_1


---

