Help with creating a db query

Notice: This thread is very old.
kc2scy
Member | 22
+
0
-

Hello,
I have the following query:

$post = $this->database->table('posts')->where('clientid', $postId);

when my latte page is getting display I'm getting this error

"Cannot read an undeclared property Nette\Database\Table\Selection::$title.":null

">Edit this post</a>
40:    <?php } ?>
41:
42:    <?php $_ctrl = $_control->getComponent("commentForm"); if ($_ctrl instanceof Nette\Application\UI\IRenderable) $_ctrl->redrawControl(NULL, FALSE); $_ctrl->render() ;
43:    }}
44:
45:    //
46:    // block title
47:    //
48:    if (!function_exists($_l->blocks['title'][] = '_lb393a2f6d06_title')) { function _lb393a2f6d06_title($_l, $_args) { foreach ($_args as $__k => $__v) $$__k = $__v
49:    ?><h1><?php echo Nette\Templating\Helpers::escapeHtml($post->title, ENT_NOQUOTES) ?></h1>
50:    <?php
51:    }}
52:

My taking of this is the field title is not in the dataset.
I'm wondering if the above query is retuning all fields?

Last edited by kc2scy (2014-03-02 03:57)

greeny
Member | 405
+
0
-

$post variable now contains Nette\Database\Table\Selection which is something with filtered results. The easiest way to get the results is to iterate them:

<?php
	foreach($post as $item) {
		echo $item->title;
	}
?>

Or if you want just the first post

<?php
	$item = $post->fetch();
	echo $item->title;
?>

But in the second case i recommend adding

<?php
	$post = $this->database->table('posts')->where('clientid', $postId)->limit(1); // added limit
?>

Last edited by greeny (2014-03-02 09:09)

kc2scy
Member | 22
+
0
-

Hi Greeny,

Thank you, that got me passed that error!

Last edited by kc2scy (2014-03-02 15:40)