We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How to do it? Dynamic table in a model

I find a Strange AND can't solve it.

<code> foreach (range(1, 3) as $key) { $dh = (new Model)->setSource('users_' . $key); $dh->findFirst(); } </code> I except get results that come from i set the table.

The Runing Sql: SELECT IF(COUNT(*)>0, 1 , 0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'users_1' AND TABLE_SCHEMA = DATABASE()【TIME:1.00ms (0.00099992752075195s)】 DESCRIBE users_1【TIME:8.00ms (0.0080001354217529s)】 SELECT users_1.id, users_1.name, users_1.email, users_1.create_time, users_1.is_delete FROM users_1 LIMIT 1【TIME:1.00ms (0.00099992752075195s)】

SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'users_2' AND `TABLE_SCHEMA` = DATABASE()【TIME:0.00ms (0s)】
DESCRIBE `users_2`【TIME:6.00ms (0.0060009956359863s)】
SELECT `users_1`.`id`, `users_1`.`name`, `users_1`.`email`, `users_1`.`create_time`, `users_1`.`is_delete` FROM `users_1` LIMIT 1【TIME:1.00ms (0.00099992752075195s)】

SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'users_3' AND `TABLE_SCHEMA` = DATABASE()【TIME:1.00ms (0.0010001659393311s)】
DESCRIBE `users_3`【TIME:8.00ms (0.0079998970031738s)】
SELECT `users_1`.`id`, `users_1`.`name`, `users_1`.`email`, `users_1`.`create_time`, `users_1`.`is_delete` FROM `users_1` LIMIT 1【TIME:1.00ms (0.00099992752075195s)】


742
Accepted
answer

SELECT IF(COUNT(*)>0, 1 , 0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'users_1' AND TABLE_SCHEMA = DATABASE()【TIME:1.00ms (0.00099992752075195s)】 DESCRIBE users_1【TIME:8.00ms (0.0080001354217529s)】 SELECT users_1.id, users_1.name, users_1.email, users_1.create_time, users_1.is_delete FROM users_1 LIMIT 1【TIME:1.00ms (0.00099992752075195s)】

SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'users_2' AND `TABLE_SCHEMA` = DATABASE()【TIME:0.00ms (0s)】
DESCRIBE `users_2`【TIME:6.00ms (0.0060009956359863s)】
SELECT `users_1`.`id`, `users_1`.`name`, `users_1`.`email`, `users_1`.`create_time`, `users_1`.`is_delete` FROM `users_1` LIMIT 1【TIME:1.00ms (0.00099992752075195s)】

SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME` = 'users_3' AND `TABLE_SCHEMA` = DATABASE()【TIME:1.00ms (0.0010001659393311s)】
DESCRIBE `users_3`【TIME:8.00ms (0.0079998970031738s)】
SELECT `users_1`.`id`, `users_1`.`name`, `users_1`.`email`, `users_1`.`create_time`, `users_1`.`is_delete` FROM `users_1` LIMIT 1【TIME:1.00ms (0.00099992752075195s)】