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
Related
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.
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.
I have a column in mysql database named time_start which type is TIME.
I am trying to format the displayed time in my view from H:i:s to simple H:i, so instead of 20:00:00 i will get 20:00.
Firstly i thought i can use format() method, but it's supposed to be used with timestamps, so of course i get error Call to a member function format() on a non-object.
I'm sure it's a simple question, but i can't solve this.
-EDIT-
Forgt to mention. I am working with a many to many relationship and i'm calling my data like:
$schedule->time_start
Here i would like to show the time in H:i format, not H:i:s.
Have you tried an accessor method? I.e. in the Schedule model,
/**
* Return a truncated version of the timestamp.
* #param $value original value of attribute
* #return string truncated value
*/
public function getTimeStartAttribute($value)
{
// Input is HH:MM:SS
return substr($value, 0, 5);
// Output is HH:MM
}
Then $schedule->time_start ought to return the time in HH:MM format.
I need to show location in Google Map.
I am using Symfony2, doctrine and MySQL.
Need help in what is the right type in yml/doctrine.
Thanks in Advance.
From the Doctrine Docs:
decimal
Maps and converts numeric data with fixed-point precision. If you need an exact precision for numbers with fractions, you should consider using this type.
As a result I use decimal for Latitude and Longitude in Doctrine. I can confirm that Decimal with a precision of 20 and a scale of 16 works.
/**
* #var string
*
* #ORM\Column(name="latitude", type="decimal", precision=20, scale=16)
*/
private $latitude;
/**
* #var string
*
* #ORM\Column(name="longitude", type="decimal", precision=20, scale=16)
*/
private $longitude;
I would use an "object" data type and save an own-typed object with the location info in it.
You could create an object named "Location" which implements an interface that forces it to have a latitude and longitude, then use:
$location = new \Location();
$location->setLatitude($lat);
$location->setLongitude($long);
$entity->setLocation($location);
Good luck!
This may be a stupid question but I might aswell as it :)
is there away to force
$tel1 = '05';// string
settype($tel1,'string');
$tel1 = 06;//either throw error, or convert it to string automatically.
var_dump($tel1);//(string [2]) 05
The above code is of the top of my head so might not be accurate but I need to keep a variable as a string not numeric, because of some silly thing I have done, now my phone numbers lose the leading 0s :-(
n I cn't rewrite it because it will mess up with other numeric types,b4 u ask it was an automated service for db to check if it was a numeric value or not,
UPDATE
This is the problem
function escape($str){
if(is_numeric($str)){
return $str;
}else{
return "'".mysql_real_escape_string($str).'\'';
}
}
$tel1 = "06";
$sql = 'SELECT * FROM blabla WHERE id = '.escape($tel1).'';
//above is same as below
$sql = 'SELECT * FROM blabla WHERE id = 06 ';
I can't change anything inside the scape function because other inputes thruout the website are using this function, I dont wanna mess their validations.
Your use of is_numeric tests for numeric content, not an integer type. But then you take a variable called $str which implies you want it to be a string.
Perhaps use:
function escape($val) {
if (is_numeric($val) && !is_string($val)) {
return $val;
}
else{
return "'" . mysql_real_escape_string($val) . '\'';
}
}
Now strings will be escaped and quoted, but not if they contain only numeric content.
you can do something like:
$string = (string) $int;
or use a function
$string = strval($int);
You can't force a variable to a specific type in the global scope.
You can force Arrays and Objects in a function.
function getElementsByClassName(DOMNode $parentElement, Array $classNames) {
...
}
If you pass an object that is not an instantiation of DOMNode (or a subclass), or if you don't pass an Array as the second argument, you'll get an error.
You can of course cast any variable, e.g. (string) $tel1.
You shouldn't be treating phone numbers as Ints anyway, because of leading zeroes and possible parenthesis and dashes. Also, once your telephone number is an Int, it won't know its 0 padding anymore because it will be discarded, so casting it back won't give you the original String.
To cast a variable you can use something like:
$i = 1;
$s = (string) $i;
Depending on the db adaptor you might not be able to detect the type being returned from the database. I believe it's PDO that returns everything (even INT values) as strings.
The number_format() function may be of use to you too.
If you declare a variable as:
$var = 06;
it immediately becomes 6 without leading zero because leading zero when it comes to integers is meaningless and therefore it's cut out.
In other words, your variable has to be created as string, which is what you probably deduced yourself.
Quick fix would be the following: you can add another parameter to your escape() function.
For example:
function escape($str, $force_str = false)
{
if($force_str)
{
// do your conversion, the rest of the site will by default pass false so nothing will be broken
}
}
As alex said, start by making sure the phone number is never converted from string to int in your own code. Then, you need to make sure it will not be converted when sent to your SQL DB.
It ought to work if you do it this way:
$sql = "SELECT * FROM blabla WHERE id = '" . mysql_real_escape_string($tel1) . "'";
This is the same as
$sql = "SELECT * FROM blabla WHERE id = '06'";