I have a problem with a project and array type in doctrine.
My entity got the field userslist who save list of user id. In my DB the field type is TEXT :
/**
* #Column(type="array", nullable=false)
*/
private $userslist;
I create my content like this :
$r->createNewLine(50, 'Name', 'Content', array(1, 5, 8))
The result in my table for my field userslist look,like this (I think it's key -> value) :
a:3:{i:0;i:1;i:1;i:5;i:2;i:8;}
I used QueryBuilder to get the results, here is my request :
$qb = $this->createQueryBuilder('r')
->andWhere('r.userslist IN (:userid)')
->setParameters(array('userid' => array(1)));
return $qb->getQuery()->getArrayResult();
I always got an empty array as result.
I have several lines in my table with userid 1 in userslist field.
a:3:{i:0;i:1;i:1;i:5;i:2;i:8;}
a:3:{i:0;i:1;i:1;i:5;i:2;i:8;}
a:3:{i:0;i:9;i:1;i:4;i:2;i:2;}
a:3:{i:0;i:1;i:1;i:5;i:2;i:8;}
a:3:{i:0;i:9;i:1;i:4;i:2;i:2;}
...
I also tried to use an expression :
$qb->add('where', $qb->expr()->in('r.userslist', array(':userid')));
The result is also empty.
But when I use LIKE it's working but not secure because I'm confuse between key and value in my table :
$qb = $this->createQueryBuilder('r')
->andWhere("r.userslist LIKE :userid")
->setParameters(array('userid' => '%i:1;%'));
How can I get my lines where the userid 1 is in my userslist field ?
Thanks
Doctrine requires you to parse the entire string to find a direct match, other wise it appears you need to use the LIKE comparison to find a record based upon a selection of the data. Here is a similar question FOS bundle - How to select users with a specific role?
To avoid this issue have you considered creating another entity and creation a relationship between that and your user object so that you don't have to rely on a LIKE condition to find your data.
Related
I'm annoyed because of a big query that goes a little in all directions.
She will use several tables:
User
Package
User Type
PackageDDLExternal (with foreign key User.id and Package.id to which I added a date attribute)
The purpose of this query is to be able to return a list that will contain:
The different existing packages with the number of times they were downloaded, for a certain period, with a given order (ASC or DESC) on the number of downloads, and all this depending on the user types whose ID table has passed as parameter.
So, I've :
Paquet :
Type Utilisateur :
User:
The manyToMany relation between TypeUtilisateur and Paquet:
And the PackageDDLExternal:
So I have already tried to create this query, but there seems to be synthax errors on the one hand (at the level of adding the order) and other things that seem to block...
public function getPackagesDDLBetween($debut, $fin,$typesUser,$ordre)
{
$qb = $this->createQueryBuilder('p');
$queryBuilder = $qb
->select("pa.titre, count(p.package)")
->join("p.package","pa")
->join("p.user","u")
->where("p.date between :debut and :fin")
->andWhere($qb->expr()->in("u.typeUser", $typesUser[0]))
->groupBy("pa.titre")
->orderBy("count(p.package) :ordre")
->setParameter('debut',$debut)
->setParameter('fin',$fin)
->setParameter('ordre', $ordre);
return $queryBuilder->getQuery()->getResult();
}
Using the $order, I get this error:
[Syntax Error] line 0, col 213: Error: Expected known function, got
'count'
But without it, my result is just null
Someone can help me please ?
EDIT: The query is almost good. The problem remains at:
->andWhere($qb->expr()->in("u.typeUser", $typesUser))
My $ tabUser is worth this:
Use an alias:
->select("pa.titre, count(p.package) as total")
Order by alias:
->orderBy("total", $ordre)
I assume that this should all be in one query in order to prevent duplicate data in the database. Is this correct?
How do I simplify this code into one Eloquent query?
$user = User::where( 'id', '=', $otherID )->first();
if( $user != null )
{
if( $user->requestReceived() )
accept_friend( $otherID );
else if( !$user->requestSent() )
{
$friend = new Friend;
$friend->user_1= $myID;
$friend->user_2 = $otherID;
$friend->accepted = 0;
$friend->save();
}
}
I assume that this should all be in one query in order to prevent
duplicate data in the database. Is this correct?
It's not correct. You prevent duplication by placing unique constraints on database level.
There's literally nothing you can do in php or any other language for that matter, that will prevent duplicates, if you don't have unique keys on your table(s). That's a simple fact, and if anyone tells you anything different - that person is blatantly wrong. I can explain why, but the explanation would be a lengthy one so I'll skip it.
Your code should be quite simple - just insert the data. Since it's not exactly clear how uniqueness is handled (it appears to be user_2, accepted, but there's an edge case), without a bit more data form you - it's not possible to suggest a complete solution.
You can always disregard what I wrote and try to go with suggested solutions, but they will fail miserably and you'll end up with duplicates.
I would say if there is a relationship between User and Friend you can simply employ Laravel's model relationship, such as:
$status = User::find($id)->friends()->updateOrCreate(['user_id' => $id], $attributes_to_update));
Thats what I would do to ensure that the new data is updated or a new one is created.
PS: I have used updateOrCreate() on Laravel 5.2.* only. And also it would be nice to actually do some check on user existence before updating else some errors might be thrown for null.
UPDATE
I'm not sure what to do. Could you explain a bit more what I should do? What about $attributes_to_update ?
Okay. Depending on what fields in the friends table marks the two friends, now using your example user_1 and user_2. By the example I gave, the $attributes_to_update would be (assuming otherID is the new friend's id):
$attributes_to_update = ['user_2' => otherID, 'accepted' => 0 ];
If your relationship between User and Friend is set properly, then the user_1 would already included in the insertion.
Furthermore,on this updateOrCreate function:
updateOrCreate($attributes_to_check, $attributes_to_update);
$attributes_to_check would mean those fields you want to check if they already exists before you create/update new one so if I want to ensure, the check is made when accepted is 0 then I can pass both say `['user_1' => 1, 'accepted' => 0]
Hope this is clearer now.
I'm assuming "friends" here represents a many-to-many relation between users. Apparently friend requests from one user (myID) to another (otherId).
You can represent that with Eloquent as:
class User extends Model
{
//...
public function friends()
{
return $this->belongsToMany(User::class, 'friends', 'myId', 'otherId')->withPivot('accepted');
}
}
That is, no need for Friend model.
Then, I think this is equivalent to what you want to accomplish (if not, please update with clarification):
$me = User::find($myId);
$me->friends()->syncWithoutDetaching([$otherId => ['accepted' => 0]]);
(accepted 0 or 1, according to your business logic).
This sync method prevents duplicate inserts, and updates or creates any row for the given pair of "myId - otherId". You can set any number of additional fields in the pivot table with this method.
However, I agree with #Mjh about setting unique constraints at database level as well.
For this kind of issue, First of all, you have to enjoy the code and database if you are working in laravel. For this first you create realtionship between both table friend and user in database as well as in Models . Also you have to use unique in database .
$data= array('accepted' => 0);
User::find($otherID)->friends()->updateOrCreate(['user_id', $otherID], $data));
This is query you can work with this . Also you can pass multiple condition here. Thanks
You can use firstOrCreate/ firstOrNew methods (https://laravel.com/docs/5.3/eloquent)
Example (from docs) :
// Retrieve the flight by the attributes, or create it if it doesn't exist...
$flight = App\Flight::firstOrCreate(['name' => 'Flight 10']);
// Retrieve the flight by the attributes, or instantiate a new instance...
$flight = App\Flight::firstOrNew(['name' => 'Flight 10']);
use `firstOrCreate' it will do same as you did manually.
Definition of FirstOrCreate copied from the Laravel Manual.
The firstOrCreate method will attempt to locate a database record using the given column / value pairs. If the model can not be found in the database, a record will be inserted with the given attributes.
So according to that you should try :
$user = User::where( 'id', '=', $otherID )->first();
$friend=Friend::firstOrCreate(['user_id' => $myId], ['user_2' => $otherId]);
It will check with both IDs if not exists then create record in friends table.
I have a user table with a property named roles, which is in Doctrine array type. This is how it looks like in the database :
roles in user table
I am creating a query using Doctrine class QueryBuilder.
Right now, my query looks like this :
$queryBuilder->select(array("u.id", "u.roles"))
->from("CNDUserBundle:User", "u")
->setMaxResults(10)
;
I need to get a list of all the users with a specific role, but I don't know how to check the value inside the Doctrine array type.
$queryBuilder->select(array("u.id", "u.roles"))
->from("CNDUserBundle:User", "u")
->where('u.role IN (:role)')
->setParameter("role",array(1,2,3))
->setMaxResults(10)
;
something like that
I am using PHP Yii framework's Active Records to model a relation between two tables. The join involves a column and a literal, and could match 2+ rows but must be limited to only ever return 1 row.
I'm using Yii version 1.1.13, and MySQL 5.1.something.
My problem isn't the SQL, but how to configure the Yii model classes to work in all cases. I can get the classes to work sometimes (simple eager loading) but not always (never for lazy loading).
First I will describe the database. Then the goal. Then I will include examples of code I've tried and why it failed.
Sorry for the length, this is complex and examples are necessary.
The database:
TABLE sites
columns:
id INT
name VARCHAR
type VARCHAR
rows:
id name type
-- ------- -----
1 Site A foo
2 Site B bar
3 Site C bar
TABLE field_options
columns:
id INT
field VARCHAR
option_value VARCHAR
option_label VARCHAR
rows:
id field option_value option_label
-- ----------- ------------- -------------
1 sites.type foo Foo Style Site
2 sites.type bar Bar-Like Site
3 sites.type bar Bar Site
So sites has an informal a reference to field_options where:
field_options.field = 'sites.type' and
field_options.option_value = sites.type
The goal:
The goal is for sites to look up the relevant field_options.option_label to go with its type value. If there happens to be more than one matching row, pick only one (any one, doesn't matter which).
Using SQL this is easy, I can do it 2 ways:
I can join using a subquery:
SELECT
sites.id,
f1.option_label AS type_label
FROM sites
LEFT JOIN field_options AS f1 ON f1.id = (
SELECT id FROM field_options
WHERE
field_options.field = 'sites.type'
AND field_options.option_value = sites.type
LIMIT 1
)
Or I can use a subquery as a column reference in the select clause:
SELECT
sites.id,
(
SELECT id FROM field_options
WHERE
field_options.field = 'sites.type'
AND field_options.option_value = sites.type
LIMIT 1
) AS type_label
FROM sites
Either way works great. So how do I model this in Yii??
What I've tried so far:
1. Use "on" array key in relation
I can get a simple eager lookup to work with this code:
class Sites extends CActiveRecord
{
...
public function relations()
{
return array(
'type_option' => array(
self::BELONGS_TO,
'FieldOptions', // that's the class for field_options
'', // no normal foreign key
'on' => "type_option.id = (SELECT id FROM field_options WHERE field = 'sites.type' AND option_value = t.type LIMIT 1)",
),
);
}
}
This works when I load a set of Sites objects and force it to eager load type_label, e.g. Sites::model()->with('type_label')->findByPk(1).
It does not work if type_label is lazy-loaded.
$site = Sites::model()->findByPk(1);
$label = $site->type_option->option_label; // ERROR: column t.type doesn't exist
2. Force eager loading always
Building on #1 above, I tried forcing Yii to always to eager loading, never lazy loading:
class Sites extends CActiveRecord
{
public function relations()
{
....
}
public function defaultScope()
{
return array(
'with' => array( 'type_option' ),
);
}
}
Now everything always works when I load Sites, but it's no good because there are other models (not pictured here) that have relations that point to Sites, and those result in errors:
$site = Sites::model()->findByPk(1);
$label = $site->type_option->option_label; // works now
$other = OtherModel::model()->with('site_relation')->findByPk(1); // ERROR: column t.type doesn't exist, because 't' refers to OtherModel now
3. Make the reference to the base table somehow relative
If there was a way that I could refer to the base table, other than "t", that was guaranteed to point to the correct alias, that would work, e.g.
'on' => "type_option.id = (SELECT id FROM field_options WHERE field = 'sites.type' AND option_value = %%BASE_TABLE%%.type LIMIT 1)",
where %%BASE_TABLE%% always refers to the correct alias for table sites. But I know of no such token.
4. Add a true virtual database column
This way would be the best, if I could convince Yii that the table has an extra column, which should be loaded just like every other column, except the SQL is a subquery -- that would be awesome. But again, I don't see any way to mess with the column list, it's all done automatically.
So, after all that... does anyone have any ideas?
EDIT Mar 21/15: I just spent a long time investigating the possibility of subclassing parts of Yii to get the job done. No luck.
I tried creating a new type of relation based on BELONGS_TO (class CBelongsToRelation), to see if I could somehow add in context sensitivity so it could react differently depending on whether it was being lazy-loaded or not. But Yii isn't built that way. There is no place where I can hook in code during query buiding from inside a relation object. And there is also no way I can tell even what the base class is, relation objects have no link back to the parent model.
All of the code that assembles these queries for active records and their relations is locked up in a separate set of classes (CActiveFinder, CJoinQuery, etc.) that cannot be extended or replaced without replacing the entire AR system pretty much. So that's out.
I then tried to see if I can create "fake" database column entries that would actually be a subquery. Answer: no. I figured out how I could add additional columns to Yii's automatically generated schema data. But,
a) there's no way to define a column in such a way that it can be a derived value, Yii assumes it's a column name in way too many places for that; and
b) there also doesn't appear to be any way to avoid having it try to insert/update to those columns on save.
So it really is looking like Yii (1.x) just does not have any way to make this happen.
Limited solution provided by #eggyal in comments: #eggyal has a suggestion that will meet my needs. He suggests creating a MySQL view table to add extra columns for each label, using a subquery to look up the value. To allow editing, the view would have to be tied to a separate Yii class, so the downside is everywhere in my code I need to be aware of whether I'm loading a record for reading only (must use the view's class) or read/write (must use the base table's class, does not have the extra columns). That said, it is a workable solution for my particular case, maybe even the only solution -- although not an answer to this question as written, so I'm not going to put it in as an answer.
OK, after a lot of attempts, I have found a solution. Thanks to #eggyal for making me think about database views.
As a quick recap, my goal was:
link one Yii model (CActiveRecord) to another using a relation()
the table join is complex and could match more than one row
the relation must never join more than one row (i.e. LIMIT 1)
I got it to work by:
creating a view from the field_options base table, using SQL GROUP BY to eliminate duplicate rows
creating a separate Yii model (CActiveRecord class) for the view
using the new model/view for the relation(), not the original table
Even then there were some wrinkles (maybe a Yii bug?) I had to work around.
Here are all the details:
The SQL view:
CREATE VIEW field_options_distinct AS
SELECT
field,
option_value,
option_label
FROM
field_options
GROUP BY
field,
option_value
;
This view contains only the columns I care about, and only ever one row per field/option_value pair.
The Yii model class:
class FieldOptionsDistinct extends CActiveRecord
{
public function tableName()
{
return 'field_options_distinct'; // the view
}
/*
I found I needed the following to override Yii's default table data.
The view doesn't have a primary key, and that confused Yii's AR finding system
and resulted in a PHP "invalid foreach()" error.
So the code below works around it by diving into the Yii table metadata object
and manually setting the primary key column list.
*/
private $bMetaDataSet = FALSE;
public function getMetaData()
{
$oMetaData = parent::getMetaData();
if (!$this->bMetaDataSet) {
$oMetaData->tableSchema->primaryKey = array( 'field', 'option_value' );
$this->bMetaDataSet = TRUE;
}
return $oMetaData;
}
}
The Yii relation():
class Sites extends CActiveRecord
{
// ...
public function relations()
{
return (
'type_option' => array(
self::BELONGS_TO,
'FieldOptionsDistinct',
array(
'type' => 'option_value',
),
'on' => "type_option.field = 'sites.type'",
),
);
}
}
And all that does the trick. Easy, right?!?
Let's say I have a model with 2 tables : Owner (int: id) and Car (int: id, int:owner_id).
I'm trying to build a validation rule on Car in order to avoid non existing owner_ids to be bound to the Car.owner_id field. I'd like to have this validation rule in the code instead of just using the DB foreign key check, because it allows me to easily display error messages on the form instead on having to process DB exceptions later.
Thus, in my model, I'd like to have something like :
public function rules() {
return array(
array('owner_id', 'in', 'range' => array(11, 12, 13)),
);
}
where 11, 12, 13 are the existing Owner ids.
I can find those ids through code like :
$ars = Yii::app()->db->createCommand("SELECT id FROM owner")->queryAll();
$ids = array();
foreach($ars as $ar) {
$ids[] = $ar['id'];
}
However, I wondered if there is any built in method in Yii allowing to get this array in a more lazy way like "$ids = Owner::model()->findIdsAsArray()" or something similar.
You dont want to do validate a range. Yii provides CExistValidator with alias exist exactly for foreign key check. The range validator may fail if the owner ids are not continuous ie if we delete an intermediate user. So You can use a validation rule as follows
array('owner_id', 'exist', 'attributeName'=>'id', 'className'=>'Owner'),
The above code validates that there exist an owner_id in the model Owner and raises an error if its not exist in the DB.