1

I use Idiorm as ORM for MySQL with PHP.

I need to check if a table is created or not.

In SQL

This works in phpMyAdmin

SHOW TABLES LIKE 'ro_globals'

What I tried in Idiorm

ORM::raw_query("SHOW TABLES LIKE 'ro_globals'")->count()

Call to undefined method admin::count()

Is it possible to make this work with Idiorm? If so, how?

5
  • So what kind of object is actually returned by raw_query? Commented Dec 18, 2012 at 12:28
  • Their own example for raw_query: $people = ORM::for_table('person')->raw_query('SELECT p.* FROM person p JOIN role r ON p.role_id = r.id WHERE r.name = :role', array('role' => 'janitor'))->find_many(); It seems like it's only to be used with the for_table. Commented Dec 18, 2012 at 12:32
  • 1
    Check this link stackoverflow.com/questions/13799559/… Commented Dec 18, 2012 at 12:34
  • Maybe I got something in my eyes, but find_many is another method than count, right? Check, which type of object you are getting back from raw_query: var_dump( get_class( ORM::raw_query( '…' ) ) ); to know which methods it provides. Just trying out random method names wouldn't lead to success. Also SELECT queries do return data in a different format than SHOW queries. Commented Dec 18, 2012 at 12:40
  • @JensTörnell You are right raw_query() is not to be used in this way, but with for_table. Recently there was some code added to the develop branch of Idiorm to support this style with a method called raw_execute(). Commented Jan 16, 2013 at 11:44

2 Answers 2

2

The correct anwser is:

ORM::forTable()->rawQuery('SHOW TABLES')->findMany()->count();

or with underscore:

ORM::for_table()->raw_query('SHOW TABLES')->find_many()->count();

Prefixed tables:

ORM::for_table()->raw_query('SHOW TABLES LIKE ' . $prefix . '%')->find_many()->count();
Sign up to request clarification or add additional context in comments.

Comments

0

I got the answer in a comment by Saharsh. It's not a Idiorm problem, it could be solved with SQL.

Result

CREATE TABLE IF NOT EXISTS

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.