I have the following function:
public function getUserMediaAction(Request $request)
{
$data = $this->decodeToken($request);
$username = $data['email'];
$user = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User')
->findOneBy(['email' => $username]);
$idUser = $user->getId();
$media = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User')
->findNewbieAllData($idUser);
return new JsonResponse(['media' => $media->getPath()]);
}
findNewbieAllData:
public function findNewbieAllData($id_newbie)
{
$sql = "SELECT m
FROM ApplicationSonataUserBundle:User u
JOIN ApplicationSonataUserBundle:Media m
WHERE u.id=?1";
$query = $this->getEntityManager()
->createQuery($sql)
->setParameter(1, $id_newbie);
$result = $query->getArrayResult();
return $result;
}
which unfortunately returns the following error:
Call to a member function getPath() on array
Stack Trace
in src\CoolbirdBundle\Controller\UserController.php at line 97 -
$media = $this->getDoctrine()->getRepository('ApplicationSonataUserBundle:User')
->findNewbieAllData($idUser);
return new JsonResponse(['media' => $media->getPath()]);
}
Would anyone have any idea of how I can solve this problem, please?
thank you in advance
Try the following instead:
return new JsonResponse(['media' => $media[0]->getPath()]);
Edit: Looking at the var_dump output you should try this:
return new JsonResponse(['media' => $media[0]['path']]);
Method findNewbieAllData hydrates data with arrays.
$result = $query->getArrayResult();
return $result;
Look at getArrayResult method here:
https://github.com/doctrine/doctrine2/blob/master/lib/Doctrine/ORM/AbstractQuery.php
Use getResult method instead.
Related
Hey i am novice in CI so please forgive! I am trying to join 2 tables in codeigniter and i am getting here these error in my code
Call to undefined method CI_DB_mysql_driver::row_array() in
C:\xampp\htdocs\Hostel\application\models\payfees.php on line 16.
My Code for the method is here like these
public function payu($id,$month){
$where = "where generatebills.student_id='".$id."' and generatebills.month='".$month."'";
$query = $this->db->select('*')
->from('generatebills')
->join('student','student.student_id=generatebills.student_id')
->where($where);
return $query->row_array();
}
Don't forget the missing ->get() method. Plus, remove the WHERE in the string:
$where = "name='Joe' AND status='boss' OR status='active'";
http://www.codeigniter.com/userguide2/database/active_record.html
I'd suggest use an array instead:
public function payu($id,$month)
{
// $where = "generatebills.student_id='".$id."' and generatebills.month='".$month."'";
$where = array(
'generatebills.student_id' => $id,
'generatebills.month' => $month,
);
$query = $this->db->select('generatebills.*')
->from('generatebills')
->join('student','student.student_id = generatebills.student_id')
->where($where);
return $query->get()->row_array();
// ^
}
I am working on a project using symfony2. I already have my search function and it is already working. But what I want is to have pagination on the search results since there are times that there are a lots of data being retrieve. How can I possibly do it?
Here is my code :
public function searchAction (Request $request) {
$request = $this->getRequest();
$data = $request->get('search');
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery (
'SELECT a,b,c FROM SampleBundle:transactDetail a
JOIN a.transact b
JOIN b.transactType c
WHERE b.pNumber LIKE :data
OR b.senderId LIKE :data'
)
->setParameter('data', "%$data%");
$result = $query->getResult();
if ($result == null ) {
return $this->render('SampleBundle:Sample:noresult.html.twig');
}
return $this->render('SampleBundle:Sample:search.html.twig', array('result' => $result));
}
Thanks in advance.
This is an example of your code with Paginator :
public function searchAction(Request $request, $firstResult = 0, $maxResults = 100)
{
$request = $this->getRequest();
$data = $request->get('search');
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
'SELECT a,b,c FROM SampleBundle:transactDetail a
JOIN a.transact b
JOIN b.transactType c
WHERE b.pNumber LIKE :data
OR b.senderId LIKE :data'
)
->setParameter('data', "%$data%")
->setFirstResult($firstResult)
->setMaxResults($maxResults);
$result = new Paginator($query);
if ($result == null) {
return $this->render('SampleBundle:Sample:noresult.html.twig');
}
return $this->render('SampleBundle:Sample:search.html.twig', array('result' => $result));
}
And if you would like to count the total just count($result);.
Paginator has Countable et IteratorAggregate interfaces.
So you can loop on your result without problem.
I am newbie in codeigniter. If I have a model like this :
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->row();
}
And I try to accessed it from may controller :
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
How to Generating Query Results, thanks for the help.
EDIT
I am new in CI, my question is : let's say I want to get the 'nama_user' into an a variable like this :
foreach ($data as $d) {
$name = $d['nama_user'];
}
echo json_encode($name);
I use firebug, it gives me null. I think my foreach is in a problem
In order to return an array from your model call you can use result_array() as
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->result_array();//<---- This'll always return you set of an array
}
Controller File with your query code
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
//Generating Query Results like this because you use $query->row();
$data->nama_user;
$data->keluhan;
$data->email;
$data->addresed_to;
$info_json = array("nama_user" => $data->nama_user, "keluhan" => $data->keluhan, "email" => $data->email, "addresed_to" => $data->addresed_to);
echo json_encode($info_json);
}
MODEL.PHP
public function get_data_for_reminder($id) {
$this->db->select('nama_user, keluhan, email, addresed_to');
$query = $this->db->get_where('tbl_requestfix', array('id_request' => $id));
return $query->row_array();
}
//Generating Query Results if use $query->row_array(); in model file
Controller.php
function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
foreach($data as $row)
{
$myArray[] = $row;
}
echo json_encode($myArray);
You fetched data for selected id it means you get a single row, if you want to get "nama_user" then in your controller:
public function reminderIT() {
$id = $this->input->post('id');
$data = $this->model_request->get_data_for_reminder($id);
$name = $data->nama_user;
echo json_encode($name);
}
I have the following piece of Laravel Code:
public function remove($user_id)
{
//$result = $this->where('id','=',$user_id)->delete();
DB::transaction(function($user_id)
{
$item_subscriber = new ItemSubscriber();
$result = $this->where('id','=',$user_id)->delete();
$result = $item_subscriber->where('user_id','=',$user_id)->delete();
});
}
I am not getting clue the reason of this error.
Ok found out the mistake I was doing;I was not using use to pass parameter.
DB::transaction(function () use ($user_id)
{
$item_subscriber = new ItemSubscriber();
$result = $this->where('id','=',$user_id)->delete();
$result = $item_subscriber->where('user_id','=',$user_id)->delete();
I have a PHP class which is supposed to return an array to where the object is instantiated. I have tried to figure out what the problem is but I can't seem to see it.
Can anyone see where I have gone wrong here or point me in the right direction?
Thanks
Here is the class (within a file called 'feeds.php.)
class updateFeed {
public function index() {
$db = JFactory::getDBO();
$query = "SELECT * FROM tweets";
$db->setQuery($query);
$result = $db->loadObject()
if(strtotime($result->date_added) <= time() - 3200) {
$this->updateTweets();
}
$query = "SELECT * FROM tweets ORDER BY tweet_date DESC LIMIT 0, 3";
$db->setQuery($query);
$results = $db->loadObjectList();
$tweet_db = array();
foreach($results as $result) {
$tweet_db[] = array(
'title' => $result->title,
'text' => $result->text,
'tweet_date' => $result->tweet_date
);
}
return $tweet_db;
}
And here is where the object is instantiated (in the index.php file):
include('feeds.php');
$tweet_dbs = new updateFeed;
print_r($tweet_dbs);
The print_r in the index file shows 'updateFeed Object ( ) '.
Thanks in advance.
You are using your class wrong. You do not call index() method.
Try this:
include('feeds.php');
// Create class instance
$instance = new updateFeed;
// Call your class instance's method
$tweet_dbs = $instance->index();
// Check result and have fun
print_r($tweet_dbs);
include('feeds.php');
$tweet_dbs = new updateFeed;
$tweets = $tweet_dbs->index();
print_r($tweets);
You missed to call the index() function..
you need to call that method using object of the class.
replace
print_r($tweet_dbs)
with
print_r($tweet_dbs->index())