I've read some posts, documentation. And still don't understand why this code doesn't work. Can someone help me to solve this stuff?
class SearchController extends Controller
{
public function actionResults()
{
$search = $this->getPost('search', '');
$criteria = new CDbCriteria();
$criteria->condition = 'username like :username';
$criteria->params = array(
':username' => '"%' . $search . '%"'
);
$results = User::model()
->findAll($criteria);
$this->render('search', array(
'search' => $search,
'results' => $results,
));
}
}
Yii profiler always show me this query:
SELECT * FROM `user` `t` WHERE username like :username
I don't understand why ":username" remains ":username" and isn't substitute with $search value.
I've solved with this solution:
class SearchController extends Controller
{
public function actionResults()
{
$results = User::model()
->findAll(array(
'condition' => 'username like :username',
'params' => array(
':username' => '%'. $this->getPost('search') .'%'
)
));
$this->render('search', array(
'search' => $this->getPost('search'),
'results' => $results,
));
}
}
But still don't understand why profiler show me incomplete query instead of the renderized query.
you can use addSearchCondition function like this,
for ref http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addSearchCondition-detail
$criteria = new CDbCriteria();
$criteria->addSearchCondition('username',$search);
class SearchController extends Controller
{
public function actionResults()
{
$results = User::model()
->findAll(array(
'condition' => 'username like :username',
'params' => array(
':username' => '%'. $this->getPost('search') .'%'
)
));
$this->render('search', array(
'search' => $this->getPost('search'),
'results' => $results,
));
}
}
Related
I have 3 models. User, role and service.
The role is associated with the user, the user with the service.
When adding a new entry to the service, I also want to add a user. Now it looks like this:
[
'label' => 'User',
'type' => 'relationship',
'name' => 'users',
'entity' => 'users',
'attribute' => 'name',
'ajax' => true,
'minimum_input_length' => 0,
],
Trait FetchOperation looks like this:
public function fetchUsers()
{
return $this->fetch([
'model' => Users::class,
'searchable_attributes' => ['name'],
'paginate' => 10,
'query' => function ($model) {
$search = request()->input('q') ?? false;
if ($search){
return $model->where('users.name', 'like', "%$search%");
}else{
return $model;
}
}
]);
}
It's work. I would like the search bar to display the role name and username for example: admin | John. But only the user name was added to the attribute.
For example
public function fetchUsers()
{
return $this->fetch([
'model' => Users::class,
'searchable_attributes' => [],
'paginate' => 10,
'query' => function ($model) {
$search = request()->input('q') ?? false;
if ($search){
return $model->selectRaw('Concat(role.name," | ", users.name) as custom_value, users.name as name')
->leftJoin('role', ...)
->where('users.name', 'like', "%$search%");
}else{
return $model;
}
}
]);
}
You can add an acessor in model like:
public function getRoleAndNameAttribute() {
return $this->role->name . '|' . $this->name;
}
And in your select define the attribute as attribute => 'roleAndName'.
Edit: add it to your model $appends property.
Cheers
I am trying to retrieve user information through api in CS-cart. But it returns only limited information. How can we modify the code to get all the user info for ex- user profiles, address, gst and all.
you can create your own API.
/var/www/html/app/addons/addon_name/Tygh/Api/Entities/Egg.php
<?php
namespace Tygh\Api\Entities;
use Tygh\Api\AEntity;
use Tygh\Api\Response;
class Egg extends AEntity
{
public function index($id = '', $params = array())
{
if(empty($id))
{
$dd=db_get_array("SELECT * FROM ?:table_name");
//result all rows
}
else
{
// for filtering purpose
$where=array("id"=>$id);
$dd=db_get_array("SELECT * FROM ?:table_name where ?w",$where);
//result-> specific one row
}
return array(
'status' => Response::STATUS_OK,
'data' => $dd
);
}
public function create($params)
{
return array(
'status' => Response::STATUS_OK,
'data' => array()
);
}
public function update($id, $params)
{
return array(
'status' => Response::STATUS_OK,
'data' => array()
);
}
public function delete($id)
{
return array(
'status' => Response::STATUS_NO_CONTENT,
);
}
public function privileges()
{
return array(
'index' => true,
'create' => 'create_things',
'update' => 'edit_things',
'delete' => 'delete_things',
'index' => 'view_things'
);
}
public function privilegesCustomer()
{
return array(
'index' => true
);
}
}
?>
Remarks:
file name,
class name,
file path
Or you can edit the user API entity from this location.
app/Tygh/Api/Entities/Users.php
Any doubts , then kick me in...
Give me an idea to call the form fields and controller in the route file and store them into the WHMCS i.e., add the client details to WHMCS. Find below the Route along with the form fields.
Route::get('/create', function () {
$users = Whmcs::AddClient([
'firstname' => Input::get('firstname'),
'lastname' => Input::get('lastname'),
'email' => Input::get('email'),
'address1' => Input::get('address1'),
'city' => Input::get('city'),
'state' => Input::get('state'),
'postcode' => Input::get('postcode'),
'country' => Input::get('country'),
'phonenumber' => Input::get('phonenumber'),
'password2' => Input::get('password2'),
'responsetype' => 'json',
]);
return $users;
});
Find below the controller code
class ClientController extends Controller
{
public function insertform(){
return view('clientlayout.main.signup');
}
public function create(){
$firstname = trim(htmlentities($_POST["firstname"]));
}
}
Perhaps the following may help point you in the right direction:
Adapt to your requirements and place inside your controller.
public function createUser($request)
{
//Create user
$newUser = new User;
$newUser->username = $request->username;
$newUser->first_name = $request->first_name;
$newUser->last_name = $request->last_name;
$newUser->email = $request->email;
$newUser->password = bcrypt($request->password);
$newUser->last_login_at = Carbon::now();
$newUser->save();
//Manually assign the role_id so no escalating privileges.
$newUser->assignRole('user');
return $newUser;
}
First of all you have to follow MVC method in laravel
Route
Route::match(['get','post'],'/create', 'ControllerName#functionname');
controller
public function create(Request $request){
$id = modelname::modelfunctionname($request->Input());
print "<pre>";
print_r ($request->input());
print "</pre>"; exit;
}
$request->input() you will get the form fields
In Your model
public static function modelfunctionname($input){
$create = DB::table('admins')->insertGetId(array(
'firstname' => $input['firstname'],
'lastname' => $input['lastname']
like this Do it for remaining field
));
return $create;
}
I want to get user_ID based on their name, and insert it into different table. Here's my controller:
$customer = strtoupper($this->input->post('cust_name'));
$address = $this->input->post('address');
$category = $this->input->post('category');
$sales = ($this->input->post('sic'));
$salesID = $this->user_model->getUserName($sales);
$result = $this->db->insert('customer', [
'cust_name' => $customer,
'cust_address' => $address,
'category' => $category,
'user_id' => $salesID
]);
And my model:
public function getUserName($sales) {
$this->db->select("user_id");
$query = $this->db->get_where('user', ['name' => $sales]);
return $query->row();
}
when I executed the query it always says "Object of class stdClass could not be converted to string"
here the var_dump:
object(stdClass)#22 (1) { ["user_id"]=> string(2) "15" }
print_r:
stdClass Object ( [user_id] => 15 )
how to solve this? thank you
hope this will help you :
use row object like this :
$salesID = $this->user_model->getUserName($sales);
$result = $this->db->insert('customer', [
'cust_name' => $customer,
'cust_address' => $address,
'category' => $category,
'user_id' => $salesID->user_id /* here is the change*/
]);
Alternate way : return user_id like this :
public function getUserName($sales) {
$this->db->select("user_id");
$query = $this->db->get_where('user', ['name' => $sales]);
return $query->row()->user_id;
}
/*then use it as you are using currently*/
first of all i have a model class like this :
class namespace_mymodule_Model_myclass
{
public function toOptionArray()
{
return array(
array('value'=>'My value',
'label'=>Mage::helper('mymodule')->__('My label name')
)
);
}
}
So i get the value from my configuration like this :
Mage::getStoreConfig('section/group/dropdown'); = 'My value'
how can i get the label ?
????????????????????; = 'My label name'
class Namespace_Mymodule_Model_Myclass
{
public function toOptionArray()
{
return array(
array(
'value' => '1',
'label' => Mage::helper('mymodule')->__('Label 1')
),
array(
'value' => '2',
'label' => Mage::helper('mymodule')->__('Label 2')
),
array(
'value' => '3',
'label' => Mage::helper('mymodule')->__('Label 3')
)
);
}
public function searchArray($myarray, $value) {
foreach ($myarray as $item) {
if ($item['value'] == $value)
return $item['label'];
}
return false;
}
}
get label from below code
$array = Mage::getModel('mymodule/myclass')->toOptionArray();
$value = 2;//search value
$label = Mage::getModel('mymodule/myclass')->searchArray($array ,$value);
echo $label;