Basically I want the same result that this SQL would return:
SELECT id FROM property_group_option opt
LEFT JOIN property_group_option_translation tra
ON opt.id = tra.property_group_option_id
WHERE opt.property_group_id = <id> AND tra.name = "<name>"
The way to go described in the Shopware documentation does not seem to get the right result. I have tried the following:
$criteria = (new Criteria())
->addFilter(new EqualsFilter('property_group_id', $propertyGroupId))
->getAssociation('property_group_option_translation')
->addFilter(new EqualsFilter('name', $value));
$result = $this->propertyGroupOptionRepository->search($criteria, Context::createDefaultContext())->first()->getId();
I did find a workaround which does not seem to be the way to go (and is also not the best performing), but it did return the right result.
$criteria = (new Criteria());
$criteria->addFilter(new EqualsFilter('name', $value));
$criteria->getAssociation('property_group_option')
->addFilter(new EqualsFilter('groupId', $propertyGroupId));
$translation = $this->propertyGroupOptionTranslationRepository->search($criteria, Context::createDefaultContext())->first()
if($translation !== null) {
$criteria = (new Criteria([$translation->get('propertyGroupOptionId')]));
$result = $this->propertyGroupOptionRepository->search($criteria, Context::createDefaultContext())->first()->getId();
}
is there a proper solution for that?
You are using the table name. Try to use the model name instead.
You have to use addAssociation() first - getAssociation() seems to return a new Criteria instance which is later not used in the actual query, if the association did not exist before:
See in the code.
public function getAssociation(string $path): Criteria
{
$parts = explode('.', $path);
$criteria = $this;
foreach ($parts as $part) {
if ($part === 'extensions') {
continue;
}
if (!$criteria->hasAssociation($part)) {
$criteria->associations[$part] = new Criteria(); // this is returned, but not stored
}
$criteria = $criteria->associations[$part];
}
return $criteria;
}
I've the following code:
Example.class.php
class Example {
public function functionArrayExample() {
$query = array();
// this foreach comes from a select that returns more than 1 results
foreach($someSelect as $exAS) {
$query[] = $exAS;
}
return $query;
}
}
index.php
require_once("Example.class.php");
$example = new Example();
$selectExample = $example -> functionArrayExample();
die(var_dump($selectExample));
But, only 1 result (in array) is showed (in die() line), why? Since the query returns more than 1 result?
If I add:
$someSelect = array();
$someSelect[] = 'red';
$someSelect[] = 'blue';
$someSelect[] = 'green';
above the foreach($someSelect as $exAS) { then your code works perfectly.
So, your error must be with your $someSelect array.
I'm solved the problem: the return returned only 1 result (and I don't know why) but with print_r the results returned correctly (2 results in array).
So.. I am practicing my PHP by writing a database object with CRUD operations.. However, I am having trouble returning a result object from the database object.
This is the relevant "select_where" function which lives inside the db Class and takes one parameter specifying the table name:
function select_where(array $values, $table, $what = "*")
{
$where_stmt = '';
if(count($values)>0)
{
$fields = array_keys($values);
$vals = array_values($values);
if ($what != "*")
{
if(is_array($what)===TRUE)
{
$what = implode($what, ',');
}
else //assume string! -- needs better error handling!
{
//Don;t do anything for now
}
}
$where_stmt = "WHERE ";
for( $i=0 ; $i<count($fields) ; $i++)
{
$where_stmt .= "$field[$i] = '$vals[$i]' AND ";
}
$where_stmt .= "$field[$i] = '$vals[$i]'";
}//else: array is an empty one, meaning SELECT ALL;
$query = "SELECT $what FROM `$table` $where_stmt;";
;
if($result = $this->mysqli_conn->query($query))
{
echo "RESULT:<br />";
print_r($result);
return $result;
}else{
return false;
}
}
The print_r function prints the attributes of the result object with no issues.. BUT when I call my function, it does not.. As so:
$db = new db();
$result = $db->select_all('codes');
print_r($result);
print_r does not print anything...
I am 100% sure that this has to do with my lack of understanding of how PHP OOP works; while I look for answers elsewhere, any suggestions or solutions are highly appreciated!
LOOKS LIKE I've Found the answer:
ROOKIE MISTAKE!
In my code, I used the helper function
$result = $db->select_all('codes');
which actually looked like this:
function select_all($table, $what="*" )
{
$empty_array = array();
$this->select_where($empty_array, $table, $what);
}
But as one can see, there was no return from THIS function! So now:
function select_all($table, $what="*" )
{
$empty_array = array();
return $this->select_where($empty_array, $table, $what);
}
is the solution!
Thank you again
i have an array with conditions i have already prepared to pass it to the query:
array:
('u.registered = 1','u.active = 0', 'u.gender = M')
when i pass to the query, it works with the number comparison but not with the varchar which is M. The error appears in "gender", it says it is a semantical error. I assume is because i am not using expr()->literal('M'), but i can't do this because the query is "already built"..
Is there an alternative way so i don't have to code all over again?
this is the code:
public function customR($data){
// var_dump($data);die();
$this->qb = $this->em->createQueryBuilder();
$andX = $this->qb->expr()->andX();
$this->qb->select('u')
->from('models\User','u');
foreach ($data as $value){
$andX->add($value);
}
$this->qb->add('where', $andX);
$query = $this->qb->getQuery();
// var_dump($query);die();
$obj = $query->getResult();
var_dump($obj);die();
if (!empty($obj)){
return $obj;
return false;
}
}
I found no way to do this, so i just changed it a little bit.
I send an array with some elements, just to have the reference of what exists and what doesn't.
So, in my Data Service I've created a function and a snippet of that function to solutionate my question was to do this:
if($key == 'gender'){
foreach($value as $key=>&$v){
$condition = ('u.gender = '. $this->qb->expr()->literal($v));
$orX->add($condition);
}
}
So I'm trying to learn how to pass arrays through a function, so that I can get around PHP's inability to return multiple values. Haven't been able to get anything to work so far, but here is my best try. Can anybody point out where I'm going wrong?
function foo($array)
{
$array[3]=$array[0]+$array[1]+$array[2];
return $array;
}
$waffles[0]=1;
$waffles[1]=2;
$waffles[2]=3;
foo($waffles);
echo $waffles[3];
For clarification: I want to be able to pass multiple variables into a function, do something, then return multiple variables back out while keeping them seperate. This was just an example I was trying to get working as a work around for not being able to return multiple variables from an array
You seem to be looking for pass-by-reference, to do that make your function look this way (note the ampersand):
function foo(&$array)
{
$array[3]=$array[0]+$array[1]+$array[2];
}
Alternately, you can assign the return value of the function to a variable:
function foo($array)
{
$array[3]=$array[0]+$array[1]+$array[2];
return $array;
}
$waffles = foo($waffles)
You're passing the array into the function by copy. Only objects are passed by reference in PHP, and an array is not an object. Here's what you do (note the &)
function foo(&$arr) { # note the &
$arr[3] = $arr[0]+$arr[1]+$arr[2];
}
$waffles = array(1,2,3);
foo($waffles);
echo $waffles[3]; # prints 6
That aside, I'm not sure why you would do that particular operation like that. Why not just return the sum instead of assigning it to a new array element?
function foo(Array $array)
{
return $array;
}
Try
$waffles = foo($waffles);
Or pass the array by reference, like suggested in the other answers.
In addition, you can add new elements to an array without writing the index, e.g.
$waffles = array(1,2,3); // filling on initialization
or
$waffles = array();
$waffles[] = 1;
$waffles[] = 2;
$waffles[] = 3;
On a sidenote, if you want to sum all values in an array, use array_sum()
I always return multiple values by using a combination of list() and array()s:
function DecideStuffToReturn() {
$IsValid = true;
$AnswerToLife = 42;
// Build the return array.
return array($IsValid, $AnswerToLife);
}
// Part out the return array in to multiple variables.
list($IsValid, $AnswerToLife) = DecideStuffToReturn();
You can name them whatever you like. I chose to keep the function variables and the return variables the same for consistency but you can call them whatever you like.
See list() for more information.
i know a Class is a bit the overkill
class Foo
{
private $sum = NULL;
public function __construct($array)
{
$this->sum[] = $array;
return $this;
}
public function getSum()
{
$sum = $this->sum;
for($i=0;$i<count($sum);$i++)
{
// get the last array index
$res[$i] = $sum[$i] + $sum[count($sum)-$i];
}
return $res;
}
}
$fo = new Foo($myarray)->getSum();
Here is how I do it. This way I can actually get a function to simulate returning multiple values;
function foo($array)
{
foreach($array as $_key => $_value)
{
$str .= "{$_key}=".$_value.'&';
}
return $str = substr($str, 0, -1);
}
/* Set the variables to pass to function, in an Array */
$waffles['variable1'] = "value1";
$waffles['variable2'] = "value2";
$waffles['variable3'] = "value3";
/* Call Function */
parse_str( foo( $waffles ));
/* Function returns multiple variable/value pairs */
echo $variable1 ."<br>";
echo $variable2 ."<br>";
echo $variable3 ."<br>";
Especially usefull if you want, for example all fields in a database
to be returned as variables, named the same as the database table fields.
See 'db_fields( )' function below.
For example, if you have a query
select login, password, email from members_table where id = $id
Function returns multiple variables:
$login, $password and $email
Here is the function:
function db_fields($field, $filter, $filter_by, $table = 'members_table') {
/*
This function will return as variable names, all fields that you request,
and the field values assigned to the variables as variable values.
$filter_by = TABLE FIELD TO FILTER RESULTS BY
$filter = VALUE TO FILTER BY
$table = TABLE TO RUN QUERY AGAINST
Returns single string value or ARRAY, based on whether user requests single
field or multiple fields.
We return all fields as variable names. If multiple rows
are returned, check is_array($return_field); If > 0, it contains multiple rows.
In that case, simply run parse_str($return_value) for each Array Item.
*/
$field = ($field == "*") ? "*,*" : $field;
$fields = explode(",",$field);
$assoc_array = ( count($fields) > 0 ) ? 1 : 0;
if (!$assoc_array) {
$result = mysql_fetch_assoc(mysql_query("select $field from $table where $filter_by = '$filter'"));
return ${$field} = $result[$field];
}
else
{
$query = mysql_query("select $field from $table where $filter_by = '$filter'");
while ($row = mysql_fetch_assoc($query)) {
foreach($row as $_key => $_value) {
$str .= "{$_key}=".$_value.'&';
}
return $str = substr($str, 0, -1);
}
}
}
Below is a sample call to function. So, If we need to get User Data for say $user_id = 12345, from the members table with fields ID, LOGIN, PASSWORD, EMAIL:
$filter = $user_id;
$filter_by = "ID";
$table_name = "members_table"
parse_str(db_fields('LOGIN, PASSWORD, EMAIL', $filter, $filter_by, $table_name));
/* This will return the following variables: */
echo $LOGIN ."<br>";
echo $PASSWORD ."<br>";
echo $EMAIL ."<br>";
We could also call like this:
parse_str(db_fields('*', $filter, $filter_by, $table_name));
The above call would return all fields as variable names.
You are not able to return 'multiple values' in PHP. You can return a single value, which might be an array.
function foo($test1, $test2, $test3)
{
return array($test1, $test2, $test3);
}
$test1 = "1";
$test2 = "2";
$test3 = "3";
$arr = foo($test1, $test2, $test3);
$test1 = $arr[0];
$test2 = $arr[1];
$test3 = $arr[2];
Another way is:
$NAME = "John";
$EMAIL = "John#gmail.com";
$USERNAME = "John123";
$PASSWORD = "1234";
$array = Array ("$NAME","$EMAIL","$USERNAME","$PASSWORD");
function getAndReturn (Array $array){
return $array;
}
print_r(getAndReturn($array));