Doctrine 2 WHERE IN - invalid input syntax for integer: "Array" - doctrine-orm

I am trying to get user accounts for specific users.
BASIC CODE:
$ownerIds = array();
foreach ($owners as $owner) {
$ownerIds[] = $owner->getId();
}
$qb = $this->entityManager
->createQueryBuilder();
THEN I tried following:
1.
$qb->select('a')
->from('Account', 'a')
->andWhere('a.owner IN (?1)')
->setParameters(array(
1 => $ownerIds
);
2.
$qb->select('a')
->from('Account', 'a')
->add('where', $qb->expr()->in('a.owner', '?1'))
->setParameters(array(
1 => $ownerIds
);
I also tried to switch question mark with parameter name ':name'
$qb->select('a')
->from('Account', 'a')
->add('where', $qb->expr()->in('a.owner', ':name'))
->setParameters(array(
':name' => $ownerIds
);
But I am still getting this error: Invalid input syntax for integer: "Array"

Try this:
$qb->select('a')
->from('Account', 'a')
->andWhere($qb->expr()->in('a.owner', $ownerIds));
Hope this helps.

Related

How do I create an object list inside an object in perl?

I'm newer in Perl, sorry for my dumb question.
I searched but I didn't found a way to do this.
I created a class that one attribute need to be a list of objects and I need to call this list later.
I tried to push the objects in a list I receive the message:
Odd number of elements in anonymous hash at...
My package
package Squid;
use strict;
use warnings;
sub new
{
my $class = shift;
my $self = {#_ };
print "Creating new $class\n";
$self->{'filas'} = [];
bless ($self, $class);
return $self;
}
1;
And this is the script:
use Squid;
use warnings;
use strict;
my $fila = FilaSquid->new("nome" => 'Fila 1', "profundidade" => 45);
my $fila2 = FilaSquid->new("nome" => 'Fila 2', "profundidade" => 7);
my $squid = Squid->new("versao" => '1.0', "origem" => 'C:\temp', "filas" => #filas);
print "\nA versao da fila eh ".$squid->{versao};
print "\nA origem da fila eh ".$squid->{origem}."\n";
foreach my $line ($squid->{filas}) {
print $line->{nome}."\n";
}
First problem is this in new.
$self->{'filas'} = [];
You're initializing $self->{'filas'} to an empty array reference. That will blow away anything passed in via new. What you probably want instead is:
$self->{filas} //= [];
Which is the same as:
$self->{filas} = $self->{filas} // [];
// is the "defined-or" operator. That says to only initialize $self->{filas} if it's undefined. It's a bit safer than using ||= because there are some valid things which are false.
Second problem is how you're passing filas to new. That you're initializing $self->{filas} with an array reference is your clue.
my #filas = (1, 2, 3, 4, 5, 6);
my $squid = Squid->new(
"versao" => '1.0',
"origem" => 'C:\temp',
"filas" => #filas
);
This is like saying:
my $squid = Squid->new(
"versao" => '1.0',
"origem" => 'C:\temp',
"filas" => 1,
2, 3, 4, 5, 6
);
You've just stuck an extra bunch of stuff on the end of what's supposed to be an even-sized list to be used as a hash.
The => operator is basically the same as a comma. It doesn't create pairs, it just allows you to leave out quotes around strings on its left hand side. key => #values isn't one pair, it's key => $values[0], $values[1], $values[2], .... new tries to treat that list as a hash, and so long as there's an even number of elements it will "work".
Instead, you need to pass in an array reference. key => \#values.
my $squid = Squid->new(
"versao" => '1.0',
"origem" => 'C:\temp',
"filas" => \#filas
);
Here's a way that you can insert numerous objects into another object without breaking encapsulation. It does not focus on any issues in your existing code, it just answers the question. In this example, Two holds numerous objects of class One. Each inner object has a name:
use warnings;
use strict;
package One; {
sub new {
my ($class, %args) = #_;
return bless {%args}, $class;
}
sub name {
my $self = shift;
return $self->{name};
}
}
package Two; {
sub new {
return bless {}, shift;
}
sub add {
my ($self, %args) = #_;
my $name = $args{name};
$self->{objects}{$name} = One->new(name => $name);
}
sub get_names {
my ($self) = #_;
return keys %{ $self->{objects} };
}
sub inner_obj {
my ($self, $name) = #_;
return $self->{objects}{$name};
}
}
package main;
my $two = Two->new;
$two->add(name => 'obj1');
$two->add(name => 'obj2');
# loop over each inner object by name
for my $name ('obj1', 'obj2'){
print $two->inner_obj($name)->name . "\n";
}
# or let Two give you the list of names back, and iterate
# automatically
for ($two->get_names){
print $two->inner_obj($_)->name . "\n";
}
Note that we're manually adding new objects in the main script. It is just as easy if you need to set up a list of objects within a module and not main... you'd just modify Two::new() to call its add() method internally.

SQL to QueryBuilder

first, sorry for bad English!
I try to convert this SQL (it's operational):
SELECT DISTINCT U.id
FROM User U
INNER JOIN Detail DE on U.id = DE.id_user
INNER JOIN matiere MA on U.id = MA.id_user
WHERE DE.ville = $var1
AND MA.matiere = $var2
in query builder.
I have try this:
$query = $repository->createQuerybuilder('U.id')
->from('User', 'U')
->innerJoin('Detail', 'DE', 'WITH', ' U.id = DE.id_user' )
->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user')
->where('DE.ville = :ville')
->setParameter('ville', $ville)
->andWhere('MA.matiere = :matiere')
->setParameter('matiere', $matiere)
->distinct();
but I have this error:
"[Syntax Error] line 0, col 49: Error: Expected end of string, got '.' "
And when I try this:
$query = $repository->createQueryBuilder()
->select('U.id')
->from('User', 'U')
->innerJoin('Detail', 'DE', 'WITH', ' U.id = DE.id_user' )
->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user')
->where('DE.ville = :ville')
->setParameter('ville', $ville)
->andWhere('MA.matiere = :matiere')
->setParameter('matiere', $matiere)
->distinct();
I have this error:
Warning: Missing argument 1 for Doctrine\ORM\EntityRepository::createQueryBuilder(),
I work with doctrine and symfony3.
Thanks for help.
This is like a syntax error , both the function of development in the query builder as the own instance of the query , try to make a debug and see how the SQL query is being built , syntax error possibly.
$qb = $repository->createQueryBuilder();
$query = $qb->getQuery();
$debug = $query->debug();
has a space between the strings [ ' U.id = DE.id_user ' ) ] , remove these spaces to try to surround the possible error;
or you can try make this way, probably work:
$query = $repository->createQueryBuilder(); <!--separte constructor this line-->
$query->select('U.id')
->from('User', 'U')
->innerJoin('Detail', 'DE', 'WITH', 'U.id = DE.id_user' ) <!--align this line-->
->innerJoin('matiere', 'MA', 'WITH', 'U.id = MA.id_user')
->where('DE.ville', ':ville') <!--separate this line-->
->setParameter('ville', $ville)
->andWhere('MA.matiere', ':matiere') <!--separate this line-->
->setParameter('matiere', $matiere)
->distinct()
->getQuery(); <!--add this line-->
$resp = $query->getResult(); <!--get the answer this line-->
In this line:
$query = $repository->createQuerybuilder('U.id')
You are trying to pass a object and method call instead of just a single parameter. That why the error about the dot.
Use this instead before the querybuilder to get the ID:
$id = U.id
or
$id = $U->getId();
then pass in parameter:
$query = $repository->createQuerybuilder($id )
...
Then you won't get the error: "[Syntax Error] line 0, col 49: Error: Expected end of string, got '.' "

Using where in salesforce query

I currently have this statment:
$query = 'SELECT DataStream__c from JUS_Contract__c limit 50';
Which works fine, but I want to only select the datastream when Datavalue = 3;
for example:
$query = 'SELECT DataStream__c from JUS_Contract__c where DataValue__c=3 limit 50';
When this is atempted I get a notice saying:
Trying to get property of non-object
I know this is because DataValue returns something like this:
stdClass Object ( [Id] => [DataValue__c] => 3)
When I do:
$query = 'SELECT DataStream__c from JUS_Contract__c limit 50';
$response = $mySforceConnection->query(($query));
//print_r($response->records);
foreach ($response->records as $record) {
print_r($record);
(for printing I fixed it by doing this: echo $record->DataStream__c; )
However I can not do that in the query, and am unsure how to change it to either a string or a int inside the query so that I can use the where command on it. Any suggestions would be great.

drupal 8 get taxonomy term value in node

Drupal\node\Entity\Node Object
(
[in_preview] =>
[values:protected] => Array
(
[vid] => Array
(
[x-default] => 1
)
[langcode] => Array
(
[x-default] => en
)
[field_destination] => Array
(
[x-default] => Array
(
[0] => Array
(
[target_id] => 2
)
)
)
Not able to get field_destination value directly. It's a taxonomy term attached with the content type. Any help appriciated.
To build on VJamie's answer.
You will need to either set a use statement at the top of your script;
use Drupal\taxonomy\Entity\Term;
Or, prefix the class instance with the namespace;
$term = \Drupal\taxonomy\Entity\Term::load($node->get('field_destination')->target_id);
That will get rid of the fatals.
You can also use some methods from EntityReferenceFieldItemList:
Gets the entities referenced by this field, preserving field item deltas:
$node->get('field_destination')->referencedEntities();
Hope it will be useful for you
The following code will get you the term object you need.
$term = Term::load($node->get('field_destination')->target_id);
If you need the name of that term you can do the following
$name = $term->getName();
Hope this helps out!
Do this
use Drupal\taxonomy\Entity\Term;
$term = Term::load($node->get('field_destination')->target_id);
$termname = $term->getName();
In drupal8 we used to follow oops approach to get the values.
This is the correct way on how to achieve it
use Drupal\taxonomy\Entity\Term;
function modulename_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
switch ($entity->bundle()) {
case 'programs':
$term = Term::load($entity->get('field_program_names')->target_id);
$name = $term->getName();
$entity->setTitle($name);
break;
}
}
entity property can be accessed directly from any reference type field.
$node = 'myNode';
$termEntity = $node->get('field_taxonomy_reference')->entity;
if ($termEntity instanceof TermInterface) {
$termLabel = $termEntity->label();
}

Nesting the result of regular expression

I'm parsing some HTML like this
<h3>Movie1</h3>
<div class="time"><span>10:00</span><span>12:00</span></div>
<h3>Movie2</h3>
<div class="time"><span>13:00</span><span>15:00</span><span>18:00</span></div>
I'd like to get result array looks like this
0 =>
0 => Movie1
1 => Movie2
1 =>
0 =>
0 => 10:00
1 => 12:00
1 =>
0 => 13:00
1 => 15:00
2 => 18:00
I can do that on two steps
1) get the movie name and whole movie's schedule with tags by regexp like this
~<h3>(.*?)</h3>(?:.*?)<div class="time">(.*?)</div>~s
2) get time by regexp like this (I do it inside the loop for every movie I got on step 1)
~<span>([0-9]{2}:[0-9]{2})</span>~s
And it works well.
The question is that: is there a regular expression that gives me the same result in only one step?
I tried nested groups like this
~<h3>(.*?)</h3>(?:.*?)<div class="time">((<span>(.*?)</span>)*)</div>~s
and I got only the last time of every movie (only 12:00 and 18:00).
With DOMDocument:
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodeList = $xpath->query('//h3|//div[#class="time"]/span');
$result = array();
$currentMovie = -1;
foreach ($nodeList as $node) {
if ($node->nodeName === 'h3') {
$result[0][++$currentMovie] = $node->nodeValue;
continue;
}
$result[1][$currentMovie][] = $node->nodeValue;
}
print_r($result);
Note: to be more rigorous, you can change the xpath query to:
//h3[following-sibling::div[#class="time"]] | //div[#class="time"]/span