I want to get everyone who has a null field.
I tried the following but it doesn't return anything.
$plato = $this->Platos->find('all',[
'conditions' => ['alergia_id' => null]
]);
In SQL null is not equal (=) to anything—not even to another
null. According to the three-valued logic of SQL, the result of
null = null is not true but unknown. SQL has the is [not] null
predicate to test if a particular value is null.
https://modern-sql.com/feature/is-distinct-from
This is the solution.
$plato = $this->Platos->find('all',[
'conditions' => ['alergia_id IS null']
]);
Related
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.
Hello? I'm building an app using the flutter provider pattern. And I created a process to query the values inside the object. I also have data in my model dart file.
Check the code below.
List<Device> _devices = [
Device(one: 'apple', two: 'iphone'),
Device(one: 'samsung', two: 'galaxy')
];
String Query(String value) {
return _media.where((medium) => medium.one == value)
.map((medium) => (medium.two)).toString();
Query("apple")
So, when I call that function, I expect iphone to be returned. But the results come in (iphne). Actually I know why. After all, the data returned is a List<Device> type. But what I want is to remove the parentheses by returning only the first value in the queried list(meaning only queried list, not the full list). In other words, I want to receive iphone, not (iphone). Currently, I am using substring removing the first and the final word, which seems to have some limitations. Is there any way to remove parentheses in that logic?
You have parentheses because you're calling .toString() on a list:
return _media.where((medium) => medium.one == value)
.map((medium) => (medium.two))
.toString();
To return just .two or the first found object, you just have to do:
return _media.firstWhere(
(medium) => medium.one == value, orElse: () => null)?.two;
That will return the value of .two of the first found object or null if nothing found.
Doc: Iterable.firstWhere()
I have the following validation,
it should match string with letters,numbers,dashes. And empty input should also be valid.
The normal string validation is ok, but I can not make it match "empty" input.
'letter_code' => 'regex:/^[A-Za-z0-9\-]*$/'
letter_code format is invalid
tests:
"C14" // valid
"3.14" // "format is invalid", as expected
"-" // valid
"" // "format is invalid", NOT expected
I just found out in laracasts forum, that there is a nullable rule.
You can read about it in the official docs.
Without the nullable rule empty strings are considered invalid if there is a regex rule as well.
If you don't add required as additional validator empty string must pass
Here is phpunit test:
/** #test */
public function letterCode()
{
$trans = new \Illuminate\Translation\Translator(
new \Illuminate\Translation\ArrayLoader, 'en'
);
$regex = 'regex:/^[A-Za-z0-9\-]*$/';
$v = new Validator($trans, ['x' => 'C14'], ['x' => $regex]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => '3.14'], ['x' => $regex]);
$this->assertFalse($v->passes());
$v = new Validator($trans, ['x' => '-'], ['x' => $regex]);
$this->assertTrue($v->passes());
$v = new Validator($trans, ['x' => ''], ['x' => $regex]);
$this->assertTrue($v->passes());
}
This is tested with Laravel 5.5
I was facing the same problem in Laravel 7.x using GraphQL Types. I needed something similar to this in a field called phone or nothig (empty string). So I did something like this:
'phone' => [
'name' => 'phone',
'type' => Type::string(),
'rules' => ['regex:/^[A-Za-z0-9\-]*$/','nullable']
Here, phone is the name of the field you want to enter text into, and rules is what we define as regex, or NULL.
Hope this helps someone!
In Laravel 5.1, I used to do a validation check to see if a posted value is set as true or false using Regex Match. The posted value is something like this:
acceptTerms true
I then check the value from Laravel validation like this:
$validator = Validator::make($postData, [
'acceptTerms' => ["regex:(true|false)"],
]);
Since updating to Laravel 5.3, the above validation rules fails and always throws the error: acceptTerms format is invalid..
Why is it failing and how do I check if a value is set as true or false in Laravel 5.3?
EDIT 1:
I forgot to add, the data posted is sent as JSON.stringify(data) and in Laravel I receive it as Input::json('data'). Would that change anything?
EDIT 2:
dd($postData) returns this:
array:31 [
"id" => "8"
"appId" => "1"
"name" => "Asdsad"
"acceptTerms" => true
]
If it's boolean, you can use boolean.
I guess it's more readable than regex.
The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and "0".
You can try this with in() as:
'acceptTerms' => 'in:true,false',
I have big fat QueryBuilder that generates sql like in this question. Apparently, all those leftJoins makes my query to crawl.
So I figured I need to fetch only the IDs that match it and later, append
$ids = $cloneOfqb->select("o.id")->resetDqlPart("join")....->getResult() ;
return $qb->andWhere("o.id IN (:ids)")->setParameter("ids", $ids)
The problem:
This would work nicely but resetDqlPart("join") removes all joins, both leftJoin and innerJoin. I use innerJoins a lot as it makes code readable. Is there a way I can reset only leftJoins but leave innerJoins? It doesn't have to be QueryBuilder object, it can be a child like Doctrine\ORM\Query if needed.
There is no way to reset only certain Joins.
I found a little discussion here, and the proposed solution (although no source code is provided) is to override the resetDqlPart() function in Doctrine's QueryBuilder.php.
Dumping $this->_dqlParts['join'] inside that function, the structure for joins appears to be the following: (my example query has an inner join and a left join, and 'e', 'ed', 'eaf' are table aliases)
array (size=1)
'e' =>
array (size=2)
0 =>
object(Doctrine\ORM\Query\Expr\Join)[666]
protected 'joinType' => string 'LEFT' (length=4)
protected 'join' => string 'e.date' (length=6)
protected 'alias' => string 'ed' (length=2)
protected 'conditionType' => null
protected 'condition' => null
protected 'indexBy' => null
1 =>
object(Doctrine\ORM\Query\Expr\Join)[654]
protected 'joinType' => string 'INNER' (length=5)
protected 'join' => string 'e.area_formativa' (length=16)
protected 'alias' => string 'eaf' (length=3)
protected 'conditionType' => null
protected 'condition' => null
protected 'indexBy' => null
You can use "getDqlPart('join')" from query builder to get applied joins, reset them via "resetDqlPart('join')" and then manually remove it from array and add it again through method "add". Not very convenient, but yet possible. Short example:
$joinDqlPart = $queryBuilder->getDQLPart('join');
$queryBuilder->resetDQLPart('join');
unset($joinDqlPart['alias'][0]);
foreach ($joinDqlPart['alias'] as $join) {
$queryBuilder->add('join', [$join->getAlias() => $join], true);
}