Monthly Archives: January 2014

How to not lose the autocomplete magic in your MCV views.

Like most MCV frameworks the controller passes the variable to the view.

Something like this in codeigniter:


$data["user"] = User::find($id);
$this->load->view('user/profile', $data);

Or Laravel:


$data["user"] = User::find($id);
return View::make('user.profile', $data);

Now if we would try to get some autocomplete the $user var in view we get nothing.

autocomplete, no suggestion

Was it getName(), getSurname() … no I’m pretty sure it was getLastName()..Ah we’ll need to look it up in the Class.

What! But Why? I’ve paid good money for this IDE and it’s awesome autocomplete function, why doesn’t it work??

First of all, don’t blame your IDE dummy. How would it know wat those resources are? It doesn’t.

Now heres the magic how it does:

Just add the @var PHPDoc to your view, just as you would in a Class.


/*@var $user User*/

No more “no suggestions”.

Whoohoo it works!

Whoohoo it works!

PS. MOST IDE will make the magic happen, the only one I’ve tested which doesn’t is Netbeans. Be free to add more in comments.

Tagged , , ,

How to check when a field value from one table does not exists in another table

Let’s say we need to list every person which does not have an account.

Thus a person record that does not have a account record.

There’s multiple ways of doing this, each way may be more efficient depending on table sizes.

NOT IN
The easiest but not necessarily the most efficient depending on the account table size.

SELECT *
FROM Persons
WHERE id NOT IN (SELECT person_id FROM accounts)

NOT EXISTS
The title says it, and it’s designed specially for our goal

SELECT *
FROM Persons
WHERE NOT EXISTS
(SELECT person_id
FROM accounts
WHERE accounts.person_id = persons.id)

LEFT OUTER JOIN (IS NULL)
The left outer join is probably quickest in the general case as it prevents repeated execution of the subquery.

SELECT *
FROM Persons
LEFT OUTER JOIN accounts
ON (persons.id = accounts.person_id)
WHERE accounts.person_id IS NULL
Tagged , ,