WP_Query meta_query REGEXP - regex

I am pretty much below beginner level for REGEX/REGEXP and have hit a blocking point in a project I am working in, where I am trying to get the ids for posts that match the search criteria , but I want to restrict the search between 2 sub-strings. I am trying to figure out is how to write the REGEXP in the meta_query:
$args = array(
'post_type'=> 'custom',
'order' => 'DESC',
'posts_per_page' => 10,
'paged' => $page,
'meta_query' => array(
array(
'key' => 'key',
'value' => "title*".$search."*_title",
'compare' => 'REGEXP',
)
),
);
And an example of the field in the DB :
a:302:{s:5:"title";s:10:"Test title";s:6:"_title";s:19:"
Unfortunately none of the combinations I tried based on documentation of SQL REGEXP won't return any values and I am trying to understand how I can pull this off and would appreciate any input.
Also would rather stick to WP_Query for now even though an SQL LIKE "%title%{$search}%_title%" works perfectly , so an alternative solution would be how to set the compare to 'LIKE' and parse it '%' since that is not possible out of the box as the % get escaped I believe.

Related

Missing creative spec facebook

I'm trying to create an ad using the php sdk.
I can create the campaign, targeting, adset, and the creative (which returns a creative_id that I can validate using the graph explorer).
But when I finally run the code to create the ad itself, I get an exception that looks like this:
"error_user_title" => "Missing creative spec"
"error_user_msg" => "No creative spec found for given adgroup."
I just can't find anything referring to this error.
Below is the relevant portion of my code:
$link_data = new AdCreativeLinkData();
$link_data->setData(array(
AdCreativeLinkDataFields::LINK => $route,
AdCreativeLinkDataFields::MESSAGE => $petition_statement,
AdCreativeLinkDataFields::NAME => $banner_title,
AdCreativeLinkDataFields::IMAGE_HASH => $image_hash,
));
$object_story_spec = new AdCreativeObjectStorySpec();
$object_story_spec->setData(array(
AdCreativeObjectStorySpecFields::PAGE_ID => $pageid,
AdCreativeObjectStorySpecFields::INSTAGRAM_ACTOR_ID=>$instagram_id,
AdCreativeObjectStorySpecFields::LINK_DATA=>$link_data
));
$creative = new AdCreative(null,$account_id);
$creative->setData(array(
AdCreativeFields::TITLE => $banner_title,
AdCreativeFields::BODY => $banner_subtitle,
AdCreativeFields::IMAGE_HASH => $image_hash,
AdCreativeFields::OBJECT_TYPE => 'SHARE',
AdCreativeFields::OBJECT_STORY_SPEC=>$object_story_spec
));
$creative->create();
echo 'Creative ID: '.$creative->id . "\n";
$ad = new Ad(null, $account_id);
$ad->setData(array(
AdFields::NAME => $short_name,
AdFields::ADSET_ID => $adset->id,
AdFields::CREATIVE => $creative,
AdFields::TRACKING_SPECS => array(array(
'action.type' => 'offsite_conversion',
'fb_pixel' => $pixel_code,
))
));
$ad->create(array(Ad::STATUS_PARAM_NAME => Ad::STATUS_PAUSED));
Appreciate any help.
I've often said that the only skill you need to be a successful developer is the ability to agonize over a problem for days, read through source code, google it, refactor, rewrite and then realize you forgot something fucking obvious.
AdFields::CREATIVE => $creative,
should read
AdFields::CREATIVE => $creative->id,
But the ability to persist isn't the skill you need. The real skill is to somehow resist the overwhelming urge to chuck your computer out the window and do something productive with your life instead.
After hours of testing it seems Trevor's answer is incorrect. This is the right syntax:
AdFields::CREATIVE => array('creative_id'=>$creative->id)

Yii2 MaskedInput / MaskedMoney for Integer Only money storage

In my database I store money as an integer, similar to Stripe. For instance 12.99 is stored as 1299. Basically taking the number and dividing by 100.
I'm trying to incorporate this with Yii2 activeForm, specifically using either the MaskInput or MaskMoney widgets. However, I cannot figure out how to configure either widget so that when the value is "1299" it displays as "12.99". So if a person types in "1200" it displays as "12.00".
This is for a simple frontend solution and not backend so I'm not looking for Yii scenarios or behaviors in this question.
I'm not sure if I understood correctly, but this is the closest I've ever got to what you want.
<?php echo \yii\widgets\MaskedInput::widget([
'name' => 'masked-input',
'clientOptions' => [
'alias' => 'decimal',
'digits' => 2,
'digitsOptional' => false,
'radixPoint' => '.',
'groupSeparator' => ',',
'autoGroup' => true,
'removeMaskOnSubmit' => true,
],
]); ?>
The behaviour of the decimal part is what bothers me, but it works as intended.

CakePHP reading Cookie with multiple dots

I am using CakePHP to develop a website and currently struggling with cookie.
The problem is that when I write cookie with multiple dots,like,
$this->Cookie->write("Figure.1.id",$figureId);
$this->Cookie->write("Figure.1.name",$figureName);`
and then read, cakePHP doesn't return nested array but it returns,
array(
'1.id' => '82',
'1.name' => '1'
)
I expected something like
array(
(int) 1 => array(
'id'=>'82',
'name'=>'1'
)
)
Actually I didn't see the result for the first time when I read after I write them. But from second time, result was like that. Do you know what is going on?
I'm afraid it doesn't look as if multiple dots are supported. If you look at the read() method of the CookieComponent (http://api.cakephp.org/2.4/source-class-CookieComponent.html#256-289), you see this:
277: if (strpos($key, '.') !== false) {
278: $names = explode('.', $key, 2);
279: $key = $names[0];
280: }
and that explode() method is being told to explode the name of your cookie into a maximum of two parts around the dot.
You might be best serializing the data you want to store before saving and then deserializing after reading as shown here: http://abakalidis.blogspot.co.uk/2011/11/cakephp-storing-multi-dimentional.html

Can Netbeans use regex to make my own 'format code' functions?

I was searching for a function, or a 'format code' function in NetBeans to format my arrays in PHP in a proper way.
E.g.:
$arr1 = array('allow',' => array('index', 'admin', 'view', 'create', 'update', 'delete'),' => array('#'),), array('deny', 'users' => array('*'),);
^^ Is quite hard to read. So if there would be a function to search for:
everytime there is a array( followed by ' replace it with an array(\n'.
or
everytime there is ,\s'\w+' followed by => replace ,\s' with ,\s\n'
Does anyone know if there is a plugin for that? (Btw, I'm not a star in regex so I'm just looking for the array() format problem.)

Doctrine findBy with OR condition

Is it possible to use OR statement in Doctrine findBy() method?
I know that given array is interpreted as case1 AND case2...
Like this
$this->repos['notif']->findBy(array('status' => 1, 'status' => 2, 'status' => 3);
Stands for
SELECT * FROM `notif` WHERE status=1 AND status=2 AND status=3;
Now I need something to stand for:
SELECT * FROM `notif` WHERE status=1 OR status=2 OR status=3;
Is there a way to get all cases?
You can write:
$this->repos['notif']->findBy(array('status' => array(1, 2, 3)));
and that should work too.
I know that this is old question. Anyways, it's possible to use Criteria for the complex queries (in Doctrine 2 at least):
$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria
->orWhere($criteria->expr()->contains('domains', 'a'))
->orWhere($criteria->expr()->contains('domains', 'b'));
$groups = $em
->getRepository('Group')
->matching($criteria);
As far as I know this is not a supported feature by Doctrine to use IN() queries with findby. You could do two things:
Implement a findByStatus(array $statusTypes) method in your (custom) 'notif' repository class. If you like this approach I can give you a example.
Convert your findBy to the following:
$qb = $this->repos['notif']->createQueryBuilder('n');
$data = $qb->where($qb->expr()->in('status', array(1,2,3)))->getQuery()->getResult();
That should work either :)
If you are using MongoDB and need more complex queries, like "less than" linked together with OR, but cannot use a query builder, this also works with this syntax:
->findBy(array(
'$or' => array(
array('foo' => array('$lt' => 1234)),
array('$and' => array(
array('bar' => 45678),
array('baz' => array('$lt' => 89013))
))
)
));
Or as a solution for your question:
->findBy(array(
'$or' => array(
array('status' => 1),
array('status' => 2),
array('status' => 3),
)
));