query in plain php is
$p = "SELECT * FROM followusers WHERE (follower='$followed1 and
followed='$follower1') or (follower='$follower1' and
followed='$followed1')"
i have written query in yii2 is
$y = Followusers::find()->where(['follower' => $userid ] and
['followed' => $conid])->orwhere(['followed' => $userid ] and
['follower' => $conid])-> all();
but i m not getting the required result for the query in yii2
Try this way:
Followusers::find()->where(['follower' => $userid])
->andWhere(['followed' => $conid])
->orwhere(['AND',
['followed' => $userid],
['follower' => $conid]
])
-> all();
If followusers table model is FollowUsers (in the current namespace, otherwise you have to prefix with namespace), you can
$data = FollowUsers::find()
->where(['AND', 'follower='.$followed1, 'followed='.$follower1])
->orWhere(['AND', 'follower='.$follower1, 'followed='.$followed1])
->all();
Otherwise, you can usign parameters:
$data = FollowUsers::find()
->where('(follower=:followed1 AND followed=:follower1) OR (follower=:follower1 AND followed=:followed1)', [':followed1' => $followed1, ':follower1' => $follower1])
->all();
It seems like you have some syntax errors in your code, try formatting it as follows:
$y = Followusers::find()
->where(['follower' => $userid, 'followed' => $conid])
->orwhere(['followed' => $userid, 'follower' => $conid])
->all();
Related
The given syntax can insert the given values but i cannot retrieve the value with method. do anyone knoe how can i do that?
$supplier = array(
'cname' => $request->getPost('cname'),
'clname' => $request->getPost('clname'),
'tlidt' => $request->getPost('tlidt'),
'tledt' => $request->getPost('tledt'),
'pname' => $request->getPost('pname'),
'tla' => $request->getPost('tla'),
);
$builder = $db->table('suppliers');
$builder->insert($supplier);
$id = $this->$db->insert_id();
Try inserting like that
$this->db->insert('suppliers',$supplier);
$id = $this->db->insert_id();
or try removing $ sign from your last line 'db'.
My mongoDB have field: $creatime, I want add field to function php.
How do it?
$ops = array(
array('$match' => $query),
array('$addFields' => array('date_code' => functionPHP('$create_time'))),
);
$results = $col->aggregate($ops,array('cursor' => array('batchSize' => 200)));
Can someone please point out my error, trying to sum a field(amount) using userId field?
https://fatfreeframework.com/3.6/mongo-mapper
Here is my code, this is returning me all documents matched with userId but not the sum.
$f3 = \Base::instance();
$mapper = new \DB\Mongo\Mapper($f3->get('MongoDB'),'transactions');
$filter = array('userId'=>'452');
$options = array(
array(
'group' => array(
'_id' => array('userId' => $userId),
'amount' => array('$sum' => 'amount')
)
)
);
$data = $mapper->find($filter, $options);
echo "<pre>";
print_r($data);
exit;
The mongo mapper uses the db.collection.group() method rather than the aggregation framework. Therefore you won't be able to call accumulators such as $sum.
Instead you must use the following syntax:
$group = [
'keys'=>['userId'=>1],
'initial'=>['sum'=>0],
'reduce'=>'function(obj,result){result.sum+=obj.amount;}',
'finalize'=>'function(result){}',
);
$data = $mapper->find($filter, ['group'=>$group]);
I would like to make an associative array using PHP for loop to use in Yii2 map() method.
The array will look like in bellow format-
$listArray = [
['id' => '1', 'name' => 'Peter/5'],
['id' => '2', 'name' => 'John/7'],
['id' => '3', 'name' => 'Kamel/9'],
];
The id and name will be changed through each iteration of the loop. Here, the name will always hold customized value after some calculation inside the loop.
Finally, the list will be used in map() method like as following
$listData=ArrayHelper::map($listArray,'id','name');
I can use map() method directly after using the Active Record to find the list array and then use that in map() method. But it does not a give me way to use custom value for the name attribute.
$listArray = UserList::find()
->where(['status' => 1])
->orderBy('name')
->all();
$listData=ArrayHelper::map($listArray,'id','name');
How can achieve this? Direct source code example would be really great for me.
Thanks in advance.
I'm assuming you want to query an ActiveRecord for data then transfer the data into a simple array.
$listData = [];
$listArray = UserList::find()
->where(['status' => 1])
->orderBy('name')
->all();
foreach($listArray as $user){
$customName = $user->name . $this->someCalculation();
$listData[] = ["id" => $user->id, "name" => $customName];
}
Or you could use the ArrayHelper class like this:
$listArray = UserList::find()
->where(['status' => 1])
->orderBy('name')
->all();
$listData = ArrayHelper::toArray($listArray , [
'app\models\UserList' => [
'id',
'name' => function ($listArray ) {
return $listArray->word . strlen($listArray->word); // custom code here
},
],
]);
I think the preferred way of doing this by defining custom calculation rule in UserList model as:
public function getCustomRuleForUser(){
// Do what ever you want to do with your user name.
return $this->name.'Your custom rule for name';
}
And use as:
$userList = UserList::find()->all();
$listData=ArrayHelper::map($userList,'id','customRuleForUser');
Now, you have your custom rule for username list in $listData.
$model_userprofile = UserProfile::find()->where(['user_id' => Yii::$app->user->id])->one();
$model_userprofile1 = UserProfile::find()
->select('user_id')
->where(['group_id' => $model_userprofile->group_id])->all();
$listData = [];
foreach($model_userprofile1 as $user){
$id = $user->user_id;
$listData[] = ["id" => $id];
}
$dataProvider = new ActiveDataProvider
([
'query' => User::find()
->select('id,username,email')
->Where(['id' => $listData])
->orderBy(['id' => SORT_DESC]),
'pagination' => ['pagesize' => 15]]);
return $this->render('index',['dataProvider'=> $dataProvider]);
is it possible to find all records in an collection where the MongoID is not in
an provided array?
Something like this (?):
$search = array(
'_id' => array('$ne' => $ids)
'readby' => array('$ne' => $userId) // works
);
Iam using PHP with the Mongo Extension.
Use $nin instead of $ne with arrays. Something like:
$search = array(
'_id' => array('$nin' => $ids),
'readby' => array('$ne' => $userId)
);
should do what you want.