Set expandable a MenuLinkTreeElement programatically - drupal-9

When loading a menuTree a MenuLinkTreeElement instance's subtree is empty when this instance is not expanded.
How to set its expanded property to 1 programmatically ?
Example:
$parameters = $menu_link_tree->getCurrentRouteMenuTreeParameters('admin');
$tree = $menu_link_tree->load('admin', $parameters);
$admin_link = reset($tree);
dpm($admin_link);
return
Drupal\Core\Menu\MenuLinkTreeElement {#242 ▼
+link: Drupal\Core\Menu\MenuLinkDefault {#261 ▶}
+subtree: []
+depth: 1
+hasChildren: true
+inActiveTrail: false
+access: null
+options: []
}

Related

Meteor aldeed collections2 update data to array of objects using for loop

Hello Now i have a problem. I want to insert data to array of objects using for loop
fields:{
type:[Object],
label:"Fields",
optional:true
},
"fields.$.category":{
type:String,
label: "Category"
},
"fields.$.sub":{
type:String,
label:"Sub Category",
},
And using savaData.js in server i tried
// ServiceProviders.update({
// '_id':"GmkGSXjyNFshomdCu"},
// {
// '$set': {'fields':{
// '$.category':categorydata,
// '$.sub':subdata
// }}
// },function(error,result){console.log(error);console.log(x+y);});
and
ServiceProviders.update(
{'_id': 'JN4mRPfJZqBadZtPY' },
{'$set': {'fields.0.category': categorydata,'fields.0.sub':subdata}},
true );
then
var x = "fields."+i+".category";
var y = "fields."+i+".sub";
x=x.toString();
y=y.toString();
ServiceProviders.update(
{'_id': 'JN4mRPfJZqBadZtPY' },
{'$set': {x: categorydata,y:subdata}},
true );
I got Different errors every time could you please help me in this issue
Currently, $ Does not work on Meteor js.
So you have to create a fields object on either server side or client side:
var fieldsData = []
for (var i = 0; i < categorydata.length || i < subdata.length ; i++ ){
fieldsData.push({
category : ( categorydata[i] ? categorydata[i] : '' ),
sub : ( subdata[i] ? subdata[i] : '' )
})
}
ServiceProviders.update(
{'_id': 'JN4mRPfJZqBadZtPY' },
{'$set': {fields : fieldsData}},
true );
Please make sure for either every filds data have cat and subcat value or make cat and subcat cat optional true.

Doctrine2: many-to-many joined entities, multiple andWhere or join WITH clauses?

Two doctrine2 entities, Photo and Tag, are linked by a many-to-many relationship, and mapped accordingly.
Each tag has a key and a value, so an example key is 'photo-type' and an example value 'people'.
I have created a custom repository PhotoRepository.php, to facilitate easy searching for photos with either an array (or a comma-separated list*) of tag pairs, extract below:
public function getQueryByTags($tags = null, $limit =0, $start =-1, $boolean ="and")
{
...
$qb->select('p')
->from('MyBundle\Entity\Photo', 'p')
->join('p.tags','t');
# ->join('a.tags','t')
if ($boolean == "and") { $qb->where('1 = 1'); }
$i = 1;
foreach ($tags as $tag) {
if ($boolean == "and") {
$qb->andWhere('t.key = ?'.$i.' AND t.value = ?'.($i+1));
} elseif ($boolean == "or") {
$qb->orWhere('t.key = ?'.$i.' AND t.value = ?'.($i+1));
}
$qb->setParameter($i, $tag['key'])->setParameter(($i+1), $tag['value']);
$i += 2;
}
...
return $qb->getQuery();
}
This works fine for a single tag. However, once tags are multiple (e.g. searching for 'photo-type'=>'people', 'person'=>'Bob'), the boolean logic breaks down and no results are return.
My suspicion is this is something to do with putting together andWhere/(orWhere) clauses from the joined Tag entity with the Doctrine2 queryBuilder(). (Since the same Tag cannot be both 'photo-type'=>'people' AND 'person'=>'Bob', although the same Photo should be).
$photos = $em->getRepository('MyBundle:Photo')->
findByTags(array(
array('key' => 'context','value' => $context),
array('key' => 'photo-type','value' => 'field'),
));
I tried to construct a JOIN WITH query instead, but this seems to require a very complex construction to create the expression, which I haven't been able to figure out:-
public function getQueryByTags($tags = null, $limit =0, $start =-1, $boolean ="and")
{
...
$qb->select('p')
->from('MyBundle\Entity\Photo', 'p');
$i = 1;
$expr = $qb->expr();
foreach ($tags as $tag) {
if ($boolean == "and") {
$expr = $expr->andX($qb->expr()->eq('t.key', '?'.$i),$qb->expr()->eq('t.value', '?'.($i+1)));
} elseif ($boolean == "or") {
}
$qb->setParameter($i, $tag['key'])->setParameter(($i+1), $tag['value']);
$i += 2;
}
$qb->join('p.tags','t',Doctrine\ORM\Query\Expr\Join::WITH,$expr);
# ->join('a.tags','t')
...
return $qb->getQuery();
}
EDIT: Ultimately the result set I want is either:
"and" search: SELECT all Photos which have (Tag with key(A) AND value(B) ) AND (another Tag with key(C) AND value(D))
"or" search: SELECT all Photos which have (Tag with key(A) AND value(B) ) OR (another Tag with key(C) AND value(D))
in which: A, B is the first unique tag 'pair' (e.g. 'photo-type'='people' or 'photo-type'='animal') and C, D is another unique tag 'pair' (e.g. 'person'='Bob', 'person'='Tiddles' or, theoretically 'animal'='Tiddles')
Can anyone help me either figure out how to construct this complex JOIN WITH expression?
Or, if it seems like I'm barking up the wrong tree, can anyone suggest an alternative more elegant way to do things?
*NB: If $tags is received as comma-separated string (e.g. $tags="photo-type=people,person=Bob") it is first converted into an array.
EDIT#2: the Tag entity, on request from #Wilt:
Tag.yml
MyBundle\Entity\Tag:
type: entity
table: tag
fields:
id:
id: true
type: integer
unsigned: true
nullable: false
generator:
strategy: IDENTITY
key:
type: string
length: 20
fixed: false
nullable: true
value:
type: string
length: 50
fixed: false
nullable: true
manyToMany:
photos:
targetEntity: Tag
mappedBy: tags
lifecycleCallbacks: { }
Photo.yml (extract only)
MyBundle\Entity\Photo:
type: entity
repositoryClass: MyBundle\Entity\PhotoRepository
table: photo
fields:
sha1:
....
manyToMany:
tags:
targetEntity: Tag
inversedBy: photos
joinTable:
name: x_photo_tag
joinColumns:
photo_sha1:
referencedColumnName: sha1
inverseJoinColumns:
tag_id:
referencedColumnName: id
Your code looks way too complicated for something like this.
You can use native doctrine solutions for this checking of your tag type with a in array solution:
$array = [];
foreach ($tags as $i => $tag) {
$array[] = $tag['value'];
}
$qb = $this->createQueryBuilder('p')
->innerJoin('p.tags','t')
->where('t.type IN(:array)')
Or am I misunderstanding your case? Then try to be a bit more clear on what result set you actually want.
EDIT
I think you can do something like this:
// Main query
$qb = $this->createQueryBuilder('p')
->innerJoin('p.tags','t');
// Get tag repository to make tag query (you can also use FROM instead)
$tagRepository = $this->getEntityManager()->getRepository('MyBundle\Entity\Tag');
// Choose your where
$where = 'orWhere'; //`OR` query:
$where = 'andWhere'; //`AND` query:
// Add a in sub query expression for each tag
foreach ($tags as $i => $tag){
$alias = 't' . $i;
$sub = $tagRepository->createQueryBuilder($alias);
$sub->where($alias . '.key = :key' . $i);
$sub->andWhere($alias . '.value = :value' . $i);
$qb->setParameter('key' . $i, $tag['key']);
$qb->setParameter('value' . $i, $tag['value']);
$qb->$where($qb->expr()->in('t.id', $sub->getDQL()));
}
// get your resultset
return $qb->getQuery();

Rails getter and setter method issue?

inside user.rb model
before_save :update_stripe
def update_stripe
if customer_id.nil? #line 1
if !stripe_token.present? #line 2
raise "Stripe token not present. Can't create account."
end
customer = Stripe::Customer.create(
:email => email, #line 3
:card => stripe_token, #line 4
)
self.role = "owner" #line 5
end
end
when i do 'role = "owner"' it doesn't sets 'role' setter but doing 'self.role = "owner"' sets it, in 'line 5', How ? Also 'customer_id', 'stripe_token', 'email' and 'stripe_token' getter works to get value without 'self' keyword in 'line 1', 'line 2', 'line 3' and 'line 4' resp. How ?
doing setter_method = val, means, you actually create a local variable inside your method and not accessing your setter, as mentioned in the link provided by Frederick Cheung.

asp.net MVC 4 webgrid with an IF statement

I have a column that needs to check a field of the object, when the field equals 2 then another has to multiply with -1.
The problem is I do not know the syntax to do this inside the creation of my gridview. Could someone give an example how this works?
#(invoice.dc.Equals(2)?String.Format("{0:0.00}", invoice.totv * -1): String.Format("{0:0.00}", invoice.totv))
This code sample i have to accomplish inside the creation of the gridview. So if field: dc equals 2 show invoice.totv * -1 else show invoice.totv.
This is what i have tried:
grid.Column("", "PDF", format:
(item) => if(#item.dc == 2)
{
String.Format("{0:0.00}", #item.totv * -1) ;
}
else
{
String.Format("{0:0.00}", #item.totv) ;
})
How about
grid.Column("", "PDF", (item) => String.Format("{0:0.00}", item.dc == 2 ? item.totv * -1 : item.totv));

how do I use the "in" operator? Flex/AS3

So the documentation has this nifty "in" operator which I like the idea of more that using a multiple step or statement (||). The documentation gives me this example.
trace("PI" in Math); // true
trace("myProperty" in Math); // false
and
public var myArray:Array = ["zero", "one", "two"];
trace(0 in myArray); // true
trace(1 in myArray); // true
trace("two" in myArray); // true
trace(3 in myArray); // false
So I try to use it like this:
var quickArray:Array = ["#icd9_color","#icd9_icd9","#templateIcd9_name","#templateIcd9_name","#templateIcd9_templateIcd9ID"];
return (element.dataField in quickArray);
Now I can trace or Alert.show() the element.datafield and it will match exactly with an array item, but it never returns true. Can anyone help me figure out why?
The only thing I can get to work this ugly thing:
return (
element.dataField == "#icd9_color" ||
element.dataField == "#icd9_icd9"
etc..
)
The in operator checks whether an object has a specified property - not what the value of that property is.
You want to use Array.indexOf and check for a non-negative value.

Resources