Trying to access the table in the controller

If $bio->user_id is equal to zero, that means that there is no user in the bios table and the link can be shown. If there is already a bio in the table then the link should be hidden. This works like it’s supposed to but what I have in the controller is wrong. I wasn’t able to find much information by googling but I think that newEmptyEntity() creates a new table instead of accessing the table like I wanted to do. What should I use instead?

in the controller:

function admin($user_id = null) {
		$bio = $this->Bios->newEmptyEntity();

In the template:

<?php if ($bio->user_id == 0) {
echo $this->Html->link("Add Bio", ['action' => 'add', $user_id]);
} ?>

This for create new entity for save or update exting one.

$this->Bios->find(‘all’)->count(); // return number of rows
$this->Bios->find(‘all’); // return all data from db

rest follow cakephp orm

Thanks debendra986 I used your code snippet and in the template I wrote:

<?php if (isset ($bio->created) ) {
echo $this->Html->link("Add Bio", ['action' => 'add', $user_id]);
} ?>

I just realized that that doesn’t make any sense. It should show the link if it’s not set not if it is set.

if (!isset($bio->created)) or if ($bio->isNew())

In the controller I have

function admin($user_id = null) {
$this->set('bio', $this->Bios->find('all'));

In the template I have

<?php if (isset($bio->created))  {
echo $this->Html->link("Add Bio", ['action' => 'add', $user_id]);
} ?>

It’s the exact opposite of what it should be because if the field ‘created’ is set, then it shows the link and the link is hidden if the variable isn’t set. So it’s backwards but it only works if I do it this way. I can’t figure out why it’s working the opposite way it’s supposed to. So right now using it the way I’m showing, it hides the link because the bio exists.

$this->Bios->find('all') will find all the bios that exist in the database. And the thing that you set for the view is a query object, not an actual Bio entity, so who can say what calling ->created on that might give you. Did you mean something like:

$this->set('bio', $this->Bios->find()->where(['Bios.user_id' => $user_id])->first());

and then in your view you can use if ($bio === null) or even just if (!$bio).

Thanks, Zuluru. What you wrote worked.