Why in yii2 phpDoc annotation in views we can see such word order
* #var $width integer
and in class's methods such word order
* #param integer $width
Why doesn't the second example have such form
* #param $width integer
The correct syntax for this tag is
#param [Type] [name] [<description>]
So declaration where type declared after parameter name is incorrect, but as you see it works the same so i guess that it has no difference but better to use syntax.
Related
I have my table information defined via the annotation of the entity. So I would like to change the following column to a TEXT instead of a STRING. So I had this:
/**
* #ORM\Column(type="string", length=255)
*/
private $category;
And I changed it to this:
/**
* #ORM\Column(type="text")
*/
private $category;
I expected it to become a TEXT, so after migrating I checked my database and it says it's a LONGTEXT. I have no idea how this is possible, but before referting my migrations, I would like to know how to make it a TEXT and not a LONGTEXT.
EDIT: I'm looking inside the migration file and I see this:
$this->addSql('ALTER TABLE cabinet CHANGE category category LONGTEXT NOT NULL');
I never typed LONGTEXT so I have no clue where Doctrine is getting that from.
The answer seems to be to give the length of TEXT as well (I thought it was defined as a fixed value), so instead of #ORM\Column(type="text") you should do #ORM\Column(type="text", length=65535). I was able to find this answer because of this other SO question.
If you want to change with migrations you need notate that converting to text will base on the current column length;
$table = $schema->getTable("tablename");
$column = $table->getColumn("columnname");
/** There you need set size of text that you want to use
* To TINYTEXT => length = 0 - 255
* To TEXT => length = 256 - 65535
* To MEDIUMTEXT = 65536 - 16777215
**/
$column->setLength('65535');
$column->setType(Type::getType(Types::TEXT))
It will change current type only if current type is not been Types::TEXT. If your text is tinytext and you need to set it to text, you need first to change type to string, set length and only then set it back to text.
How can I call assertJsonCount using an indexed, nested array?
In my test, the following JSON is returned:
[[{"sku":"P09250"},{"sku":"P03293"}]]
But attempting to use assertJsonCount returns the following error:
$response->assertJsonCount(2);
// Failed asserting that actual size 1 matches expected size 2.
This may or may not be specific to Laravel. Although a Laravel helper is involved, this issue may occur elsewhere.
assertJsonCount utilises the PHPUnit function PHPUnit::assertCount which uses a laravel helper data_get, which has the following signature:
/**
* Get an item from an array or object using "dot" notation.
*
* #param mixed $target
* #param string|array|int $key
* #param mixed $default
* #return mixed
*/
function data_get($target, $key, $default = null)
{
if (is_null($key)) {
return $target;
}
...
We can see that the JSON returned is a nested array, so logically we should pass in a key of 0.
$response->assertJsonCount($expectedProducts->count(), '0');
However this will be ignored as assertCount function checks if a key has been passed using is_null.
To overcome this, we can count all children of 0:
$response->assertJsonCount($expectedProducts->count(), '0.*');
This will produce the desired result.
I want to map a property from one of my doctrine's entity to a table's column with datatype of YEAR.
At the moment I'm using the doctrine's integer type with the length of 4 as demonstrated bellow, but I was wondering if there is a better match for such mapping.
/**
* #var integer
*
* #ORM\Column(name="year", type="integer", length=4, nullable=true)
*/
private $year;
Any exact numeric type should be ok
I am experiencing a strange error when I start my Yii2 app! Please need your help on this. Here is the what I get:
PHP Warning – yii\base\ErrorException
mb_strlen() expects parameter 1 to be string, array given
1. in /var/www/yii2/basic/vendor/yiisoft/yii2/helpers/BaseStringHelper.php at line 31
22232425262728293031323334353637383940
{
/**
* Returns the number of bytes in the given string.
* This method ensures the string is treated as a byte array by using `mb_strlen()`.
* #param string $string the string being measured for length
* #return integer the number of bytes in the given string.
*/
public static function byteLength($string)
{
return mb_strlen($string, '8bit');
}
/**
* Returns the portion of string specified by the start and length parameters.
* This method ensures the string is treated as a byte array by using `mb_substr()`.
* #param string $string the input string. Must be one character or longer.
* #param integer $start the starting position
* #param integer $length the desired portion length. If not specified or `null`, there will be
* no limit on length i.e. the output will be until the end of the strin
I happened an the same error message copying an external component in my project in my case I solved the problem by running
composer update
I have one column for the time when object is created and one for when it is updated.
When I create and persist new object I get the error from MySQL:
updated cannot be null.
I didn't set any value to it because I want updated column to remain untouched and eventually get default database value whatever it is.
How to do tell doctrine to persist only those columns which values I explicitly set/changed?
Make sure your column definition has nullable defined as true.
/**
* #Column(type="datetime", nullable=true)
*/
protected $updated;
Then you should set your column in your entity to nullable false.
In your Entity:
/**
* #var datetime $date_updated
*
* #ORM\Column(type="datetime", nullable=true)
*/
private $date_updated;