CI: Querying two tables in the model, explode - mysql

i'm thinking about this for days now and don't come to grasps (since i'm relativley new to MVC and CI). I'm not even sure whether this is an issue with MVC, MySQL or arrays.
Situation: 2 MySQL tables
Table data: id, title, list
Table values: id, name
Querying the data table results in an array like the following (excerpt):
[4] => Array
(
[id] => 3
[title] => Foo
[list] => 1,2,3,4,6,14
)
[5] => Array
(
[id] => 4
[title] => Bar
[list] => 2,6,9,12
)
The field list contains comma separated values that correspond to some IDs of the values table like
[3] => Array
(
[id] => 12
[name] => 'value12'
)
What I try to do for each row is:
take the list-values & explode it into an array
check with the result set from the values-table (via in_array() method)
return the name values of the IDs if
include it somehow into the main result set (e.g. as a 2-dimensional array):
[5] => Array (
[id] => 4
[title] => Bar
[list] => Array (
[0] => value6
[1] => value12
...
)
)
My naive approach so far was to
run a query on each of the 2 tables
compare the 2 result sets via in_array
My main problem (while trying to strictly separate model, controller and view): How can I include the name field from the values-table in the "main loop" of the data table result set?
if($q->num_rows() > 0)
{
$data[] = $q->result_array();
foreach ($q->result() as $row)
{
$data[] = $row;
}
return $data;
}
If I use the following (cumbersome) approach i naturally get a new item each time:
foreach ($q->result_array() as $row)
{
$data[]['id'] = $row['id'];
$data[]['title'] = $row['title'];
$data[]['list'] = $row['year'];
}
Since this is a MySQL database I see no way to do the explode and the comparison in SQL (with LIKE or something else).
Any hint, even a simple link to an info bit, is highly appreciated.
Thanks a trillion!
fab

There is a many-to-many relationship between lists and list values. The conventional way to model this in a relational database is to create a joining table. So I'd structure your schema like this.
lists : list_id, title
values : value_id, name
list_values : list_id, value_id
list_values is the joining table. It links lists with values.
To build a list you could have the following functions in your model
function build_list($list_id)
{
$list = $this->get_list($list_id);
$list->values = $this->get_list_values($list_id);
return $list;
}
function get_list($list_id)
{
$sql = 'select * from lists where list_id=?';
return $this->db->query($sql, array($list_id))->row();
}
function get_list_values($list_id)
{
$sql = 'select v.value_id, v.name
from list_values lv
join values v on v.value_id=lv.value_id
where lv.list_id=?';
return $this->db->query($sql, array($list_id))->result();
}

Related

Yii2 getting columns name from SQL command when result == null

If you get the following code :
$DBConnection =
CreateNewDBConnection(Yii::$app->get('db_cdh'),$aDatabaseName);
$DBConnection->open();
$command = $DBConnection->createCommand($aQuery);
$queryres = $command->queryAll();
If there is result from the query, I get an array, like this
Array
(
[0] => Array
(
[name] => 2.6.084.545
[xdim+2] => 70
)
[1] => Array
(
[name] => 2.5.102.030
[xdim+2] => 60
)
[2] => Array
(
[name] => 2.5.141.560
[xdim+2] => 80
)
)
But if the result of the query is empty, i get an empty array.
How is it possible to get the columns name ?
The reason why I'm asking this, it's because I'm asking queries to multiple DBs and some have results (1 or more lines) and otherd not. The system almost works, but the grid view parse only the first line to find the columns to display. So depending on the order result across the multiple DB, the grid view display the columns or not, depending what come first ....
Any help welcome.
You can take the column names using yii\db\TableSchema and use them afterwards:
$columns = [];
if (empty($queryres)) {
$columns = $DBConnection->getTableSchema('your_table_name')->getColumnNames();
}

cakephp resultset for multiple joins and aggregate function...i.e. complex queries

I am using cakephp in one of my project. What i need is to handle complex query using single model and single array out.Since I am new to cakephp i got stucked really very bad here :
$rs = $this->User->query("
SELECT (wd.wajebaat_amt) as commited,
SUM(pd.sila_waje) as paid,
(wd.wajebaat_amt-sum(pd.sila_waje)) as balance,
FROM wajebaat_details as wd
LEFT JOIN waje_pay_details as pd ON (pd.waje_id=wd.waje_id)
WHERE wd.hof_id="123" and wd.year="2010"
GROUP BY wd.waje_id");
print_r($rs); exit();
// it displays output as
Array
(
[0] => Array
(
[wd] => Array
(
[commited] => 252000
)
[0] => Array
(
[paid] => 253829
[balance] => -1829
)
)
)
//however i need it following format
Array
(
[0] => Array
(
[wd] => Array
(
[commited] => 252000
[paid] => 200000
[balance] => 52000
)
)
)
You can use Hash (utility) method format() in cakephp 2.5 to convert the nested array into string,in previous version of cakephp the method is set(),
Hash::format(array $data, array $paths, $format)
Example :
$result = Hash::format($rs,array('{n}.wd.commited','{n}.wd.0.paid','{n}.wd.0.balance'),'%1$d,%2$d,%$d');
Output:
252000,2000000,52000
For More formating option refere cook book of cakephp

Executing array values in a MySQL PDO statement with WHERE and OR

I've been trying to look for a solution where you can fetch database with one prepared statement and execute it with an array value
Typically I do this with my statement:
$search = $db->prepare("SELECT * FROM table WHERE name = ?");
$search->execute(array($name));
But what if i have an array like so:
Array (
[0] => Array
(
[name] => Burger Joint
)
[1] => Array
(
[name] => Burger Joint
)
[2] => Array
(
[name] => Burgers
)
[3] => Array
(
[name] => Meats
)
)
I'd like to somehow go through my database with either of the values in the array WHERE name=? in the statement. However, sometimes there's going to be multiple similar names, is there a way to condense the array before hand or what would be the best practice in a situation like this?
Thanks!
You can do this in a number of ways, but since you mentioned OR, let's use that:
First, your array of possible values. Let's take your array and mold it into an array of unique values:
$values_array = array_unique(
array_map(
function($element) {
return $element['name'];
},
$original_array
)
);
// $values_array now contains array('Burger Joint', 'Burgers', 'Meats')
Now, we build the prepared query by introducing as many placeholders as you have possible values:
$query = sprintf('SELECT * FROM table WHERE %s',
implode(
' OR ',
array_fill(
'name = ?',
count($values_array)
)
)
);
// $query now contains 'SELECT * FROM table WHERE name = ? OR name = ? OR name = ?'
and execute it:
$search = $db->prepare($query);
$search->execute($values_array);
Alternatively, you could use IN instead, building your query like so:
$query = sprintf('SELECT * FROM table WHERE name in (%s)',
implode(
', ',
array_fill(
'?',
count($values_array)
)
)
);
// $query now contains 'SELECT * FROM table WHERE name in (?, ?, ?)'
$search = $db->prepare($query);
$search->execute($values_array);
This will have the same effect, and it's slightly more clear what's going on by looking at the code.
Try name IN instead of name = .
First, you need IN. field IN (1,2) is equal to field=1 OR field=2.
Next, you need some sort of helper function, to put all that mess of technical details of creating correct SQL statements away from application business code. To make it in ONE line, not 50.
$data = $db->getAll("SELECT * FROM table WHERE name IN (?a)",$names);
Finally, it seems you're getting your names from another query.
In this case you have to run only single query using JOIN. You may ask another question under [mysql] tag providing both your queries.
To get only names into array you have to use another helper function (though you have to create it yourself or get somewhere first):
$names = $db->getCol("here goes your query to get names");

CakePHP has and belongs to many conditions with NOT EXISTS

$conditions = Array
(
[table] => products_pages
[alias] => ProductsPage
[type] => inner
[foreignKey] =>
[conditions] => Array
(
[0] => ProductsPage.product_id = Product.id
)
)
I'm trying to set up NOT EXISTS conditions, like the following SQL statement:
SELECT * FROM products_pages,products
WHERE NOT EXISTS (SELECT id
from products_pages
where products_pages.product_id = products.id)
So basically select any product that doesn't exist in the products_pages table.
What is the proper way to format that SQL statement for CakePHP and replace it here:
[conditions] => Array
(
[0] => (What's the proper way to insert above SQL here?
)
Would really appreciate your help guys, I've been trying to figure this out for about 5 hours with no luck. Thanks!
You can always use query if you don't find the way to do it with CakePHP:
http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#model-query
In this case security wouldn't be compromised as you are not using any input.
Anyway, something simple would be just to do it in more than one step:
//selecting the products in the productcs_pages table
$productsWithPages = /* query to get them*/
//getting an array of IDs
$productsWidthPagesIds = Hash::extract($productsWithPages, '{n}.Product.id');
//doing the NOT IN to select products without pages
$productsWithoutPages= $this->Product->find('all',
array('conditions' =>
array( 'NOT' => array('Product.id' => $productsWidthPagesIds )
)
);

mysql if condition in query can't get tablename from mysql_field_table

This query works:
SELECT Article.id,
Article.post_time,
Article.post_locked,
Article.comments_locked, Article.title,
IF(CHAR_LENGTH(Article.content)>2000,
RPAD(LEFT(Article.content,2000),2003,'.'),
Article.content) as content,
Article.tags, Category.*,
User.id, User.user_name,
Comment.comment_count
FROM `articles` as `Article`
LEFT JOIN `categories` as `Category` ON `Article`.`category_id` = `Category`.`id`
LEFT JOIN `users` as `User` ON `Article`.`user_id` = `User`.`id`
LEFT OUTER JOIN (SELECT article_id, count(*) comment_count FROM `comments`) as `Comment` ON `Article`.id = `Comment`.article_id
WHERE '1'='1'
ORDER BY `Article`.`id` DESC
But when I loop through the resultset to assign the table name along with the field using 'mysql_field_table', the 'content' returns a table name of nothing, while all others have their correct table:
Array (
[0] => Article
[1] => Article
[2] => Article
[3] => Article
[4] => Article
[5] =>
[6] => Article
[7] => Category
[8] => Category
[9] => User
[10] => User
[11] => Comment )
using
for ($i = 0; $i < $numOfFields; ++$i) {
array_push($table,mysql_field_table($this->_result, $i));
array_push($field,mysql_field_name($this->_result, $i));
}
Anyone ever try to do this? Have a solution? I want to return less data from my DB in my query. Or is it less intensive (on mysql, memory, cpu) to simply select all content and truncate the content via PHP? I thought returning less from DB would be better.
Thanks a bunch!!
Peace.
EDIT
to clear up, this is the result, you will see why it isnt what I want:
Array (
[0] => Array (
[Article] => Array (
[id] => 8
[post_time] => 1278606312
[post_locked] => 0
[comments_locked] => 0
[title] => Article 8
[tags] => test )
[] => Array (
[content] => my content for Article )
[Category] => Array (
[id] => 2
[name] => cat2 )
[User] => Array (
[id] => 3
[user_name] => user3 )
[Comment] => Array (
[comment_count] => 1 )
)
[1] => Array (
[Article] => Array (
[id] => 7
etc...
In order to use characters beyond the English alphabet and spaces in a column alias, the standard SQL means requires using double quotes (though MySQL supports using backticks IE: "`" too):
...,
IF(CHAR_LENGTH(Article.content)>2000,
RPAD(LEFT(Article.content,2000),2003,'.'),
Article.content) AS "Article.content",
...
no you cant use a as [tablename].[columnname]-like format for custom column names.
It would be weird anyway if it would work, because how can content be defined as 'Article.content' if it's not really part of the Article table dataset.
Just select the columns you need and join where needed.
But what's WHERE '1' = '1' doing in there? that will just evaluate to true as it is a boolean expression, but it won't affect your resultset.
But when I loop through the resultset
to assign the table name along with
the field using 'mysql_field_table',
the 'content' returns a table name of
nothing, while all others have their
correct table
Once you've done that magic on Article.content, to create the content field, it no longer belongs to the Article table. Rather, it belongs to the result set of that query. I believe that's the explanation for having no table associated with that field.
Imagine a GROUP BY query, with something like COUNT(*) as number. 'number' doesn't belong to any table.
If you really need the ability to know that the column had a particular source, could you have a view on top of Article which does this manipulation to content? Then the source would appear to be the view? Unfortunately, MySQL doesn't support declared computed columns in tables, that might also be useful to you in this case.
while ($row = mysql_fetch_row($this->_result)) {
$prev_table;
for ($i = 0;$i < $numOfFields; ++$i) {
if ($table[$i] == "") {
$tempResults[$prev_table][$field[$i]] = $row[$i];
}else {
$tempResults[$table[$i]][$field[$i]] = $row[$i];
}
$prev_table = $table[$i];
}
}
Oh well, mysql couldnt do what I wanted. I added the prev_table to take the one before ;)
Thanks to everyone for the help.