I have a class with the following attribute :
/**
* #var boolean
*
* #ORM\Column(name="validated", type="boolean", nullable=true)
*/
private $validated;
I would like it to be null by default in the database but whatever I do I keep getting a FALSE value when inserting it as NULL.
Does anybody have an idea on how to achieve this?
Default values come from your entities, so this should do it
private $validated = null;
Never used postgre but it appears that there is a function which return false for null value (convertSingleBooleanValue) as shown here :
private function convertSingleBooleanValue($value, $callback)
{
if (null === $value) {
return $callback(false);
}
if (is_bool($value) || is_numeric($value)) {
return $callback($value ? true : false);
}
if (!is_string($value)) {
return $callback(true);
}
//etc...
}
https://github.com/doctrine/dbal/pull/625
So as this is most likely your problem, you should check if there is a way to bypass this.
As for postgresql http://www.postgresql.org/docs/8.1/static/datatype-boolean.html there is still an "unknown" value that is null.
Related
i am trying web scraping so i get this error on "\vendor\symfony\dom-crawler\Crawler.php:552"
here is the crawler.php code what the browser showed me:
#throws \InvalidArgumentException When current node is empty
*/
public function text(string $default = null, bool $normalizeWhitespace = true): string
{
if (!$this->nodes) {
if (null !== $default) {
return $default;
}
throw new \InvalidArgumentException('The current node list is empty.');
}
$text = $this->getNode(0)->nodeValue;
if ($normalizeWhitespace) {
return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text));
}
return $text;
}
This exception is thrown when trying to access the text of an empty node list. I have written a util class for this purpose, as I don't always mind the value being empty. Simply pass the node list you want to get the inner text from, like so CrawlUtil::innerText($nodeList). Hope it will help somebody in the future :)
use Symfony\Component\DomCrawler\Crawler;
class CrawlUtil
{
/**
* Suppresses exception that node list is empty.
*
* #param Crawler $crawler
*
* #return string|null
*/
public static function innerText(Crawler $crawler): ?string
{
if ($crawler->count() > 0 && $crawler->text() !== '') {
return $crawler->innerText();
}
return null;
}
}
In my entity I have a property that is a value object.
class Meeting
{
/** #var MeetingStatus */
private $status
/**
* #param MeetingStatus|null $status
*/
public function setStatus(MeetingStatus $status = null)
{
$this->status = $status ?: new MeetingStatus(MeetingStatus::DRAFT);
}
}
The only way I see to both typehint and to allow null is by setting null as default parameter value. But it seems very weird to allow a setter without a required parameter.
$meeting->setStatus(); // wut?
Does anyone have an elegant solution for this?
Set public $status to MeetingStatus::DRAFT
public function __construct() {
$this->status = MeetingStatus::DRAFT;
}
and don't call setter if you have it as NULL.
OR
Always set meeting status. You can set it to MeetingStatus::UNDEFINED where const UNDEFINED = null;
You may set default value in constructor, almost like in previous answer:
private $status;
public function __construct()
{
$this->status = new MeetingStatus(MeetingStatus::DRAFT);
}
public function setStatus(MeetingStatus $status)
{
$this->status = $status;
}
Now you doesn't need to make member public or allow null in setStatus()
I have a simple problem, I can't pass data from one function to another. This is my code:
/**
* #dataProvider myProvider
*/
public function testIfPathIsString($path){
$isValid = (is_string($path)) ? true : false;
$this->assertEquals(true, $isValid);
return $path;
}
/**
* #depends testIfPathIsString
*/
public function testIfFileExists($help){
$exists = $this->fileController->checkIfFileExists($help);
$this->assertEquals(true, $exists);
return $help;
}
public function myProvider()
{
return array(
array("C:/xampp/htdocs/mycustom.txt")
);
}
First function gets path just fine but passing with annotation depends to second function fails, it sends null or empty.
I explicitly have one correct data path in dataset so that doesn't make any sense?
In my Symfony 2 (2.4.2) application, there is a Form Type which consists of 3 fields.
I'd like the validation be like this: If field A and field B are blank, field C should not be blank. This means that at least one field should receive some data.
Currently, I check the received data in the controller. Is there a more recommended way to do this?
There are even easier solutions than writing a custom validator. The easiest of all is probably the expression constraint:
class MyEntity
{
private $fieldA;
private $fieldB;
/**
* #Assert\Expression(
* expression="this.fieldA != '' || this.fieldB != '' || value != ''",
* message="Either field A or field B or field C must be set"
* )
*/
private $fieldC;
}
You can also add a validation method to your class and annotate it with the Callback constraint:
/**
* #Assert\Callback
*/
public function validateFields(ExecutionContextInterface $context)
{
if ('' === $this->fieldA && '' === $this->fieldB && '' === $this->fieldC) {
$context->addViolation('At least one of the fields must be filled');
}
}
The method will be executed during the validation of the class.
This is probably a use case for a Custom Validation Constraint. I haven't used it myself but basically you create a Constraint and a Validator. You then specify your Constraint in your config/validation.yml.
Your\Bundle\Entity\YourEntity:
constraints:
- Your\BundleValidator\Constraints\YourConstraint: ~
The actual validation is done by your Validator. You can tell Symfony to pass the whole entity to your validate method to access multiple fields with:
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
And your validate:
public function validate($entity, Constraint $constraint)
{
// Do whatever validation you need
// You can specify an error message inside your Constraint
if (/* $entity->getFieldA(), ->getFieldB(), ->getFieldC() ... */) {
$this->context->addViolationAt(
'foo',
$constraint->message,
array(),
null
);
}
}
You can do this with Group Sequence Providers, for example:
use Symfony\Component\Validator\GroupSequenceProviderInterface;
/**
* #Assert\GroupSequenceProvider
*/
class MyObject implements GroupSequenceProviderInterface
{
/**
* #Assert\NotBlank(groups={"OptionA"})
*/
private $fieldA;
/**
* #Assert\NotBlank(groups={"OptionA"})
*/
private $fieldB;
/**
* #Assert\NotBlank(groups={"OptionB"})
*/
private $fieldC;
public function getGroupSequence()
{
$groups = array('MyObject');
if ($this->fieldA == null && $this->fieldB == null) {
$groups[] = 'OptionB';
} else {
$groups[] = 'OptionA';
}
return $groups;
}
}
Not tested it but I think it would work
I know that it's old question but you can also use the NotBlankIf validator from this bundle https://github.com/secit-pl/validation-bundle
<?php
use SecIT\ValidationBundle\Validator\Constraints as SecITAssert;
class Entity
{
private ?string $fieldA = null;
private ?string $fieldB = null;
#[SecITAssert\NotBlankIf("!this.getFieldA() and !this.getFieldB()")]
private ?string $fieldC = null;
public function getFieldA(): string
{
return $this->fieldA;
}
public function getFieldB(): string
{
return $this->fieldB;
}
}
After upgrade of Codeigniter i get this message
Cannot access protected property MY_Loader::$_ci_cached_vars
i know that this property is now protected so i change
else if (isset($CI->load->_ci_cached_vars[$key]))
{
$val = $CI->load->_ci_cached_vars[$key];
}
to
if (isset($CI->load->get_var($key)))
{
$val = $CI->load->get_var($key);
}
but then i get
Can't use method return value in write context
this is get_var method
/**
* Get Variable
*
* Check if a variable is set and retrieve it.
*
* #param array
* #return void
*/
public function get_var($key)
{
return isset($this->_ci_cached_vars[$key]) ? $this->_ci_cached_vars[$key] : NULL;
}
what can i do, just use
if ($CI->load->get_var($key)) != null) {
$val = $CI->load->get_var($key);
}
without isset? i want to check if is not NULL, becouse get_var method return null
or is if ($CI->load->get_var($key))) { check enough?
You cannot use isset on a function
i.e. $CI->load->get_var($key) will always return "something" - but what that "something" is depends.
So you are correct - the code below will achieve your goal. If the function returns "null" - then isset already failed. If the function returns something else (besides null) - then you will have a valid return.
if ($CI->load->get_var($key)) != null) {
$val = $CI->load->get_var($key);
}