Can't access array members - php

I can't figure this out. I've create a simple class that returns an array of arrays. Here is the class contructor...
class BlogComments {
public $commentArray=array();
public $blogId;
function __construct($inId) {
if(!empty($inId)) {
$this->blogId=$inId;
$sql="select id,name,url,comment,email from blog_comment where blog_id=$inId";
$link2=GetConnection();
$query=mysql_query($sql,$link2) or die("Invalid blog id:".mysql_error());
while($row=mysql_fetch_array($query)) {
$this->commentArray=array(
"id"=>$row['id'],
"name"=>$row['name'],
"url"=>$row['url'],
"email"=>$row['email'],
"comment"=>$row['comment']
);
}
mysql_close($link2);
}
}
}
I'm trying to access each member of the array via a loop. It's entering the loop but the values returned are empty. I've verified that data is being written into the array. Here's my code...
include "include/commentclass.php";
$comments = new BlogComments($post->id);
foreach($comments as $comment) {
echo "<h4>".$comment->commentArray['name']."</h4>
".$comment->commentArray['url']."
<p>".$comment->commentArray['comment']."</p>";
}
Basically it returns empty tags. I've also verified that $post->id holds a valid value. Any ideas what I'm doing wrong?
Thanks for the help,
B

You are doing some mistakes, the first is the one netcoder pointed out: you are using the object as an array without implementing an Iterator interface. The second is that you are assigning directly the result array to $this->commentArray. You should append the result to the array this way: $this->commentArray[] = array(

Try this:
$comments = new BlogComments($post->id);
foreach ($comments->commentArray as $comment) {
echo "<h4>".$comment['name']."</h4>
".$comment['url']."
<p>".$comment['comment']."</p>";
}
The new keyword returns a single object. Unless your object (BlogComments) implements Traversable, foreach will act on the public properties commentArray and blogId, and not on the commentArray contents.
You could also have your class implement an Iterator interface.

Related

Symfony - Call to a member function getDestination() on string

In my Symfony project I am returning all objects with Doctrine query builder defined with Paginator.
When dumping $posts['data'], response is on the image: IMAGE
When entering the loop and dumping first result, this is what I get: IMAGE
I want on each array object to assign new key-value pair. Every array object has destinationId (you can see on the image), and I have a method that searches for the name by that param.
I want to assign that new value to every object in foreach.
Code:
$posts = $this->entityManager->getRepository(Post::class)->getPage();
foreach ($posts['data'] as $postsData) {
foreach ($postsData as $post) {
$destinationName = $this->destinationService->getDestinationNameById(
$post->getDestinationId()
);
$postsData['destinationName'] = $destinationName;
}
}
Error is:
Call to a member function getDestinationId() on string
This is very odd as this field has entity type defined as string and also when dumping
dump($post->getDestinationId());
I get: "107869901061558" which is string.
It's because you override you $postsData variable.
You need to use a different variable to store your $destinationName like this :
$posts = $this->entityManager->getRepository(Post::class)->getPage();
$destinationNames = [];
foreach ($posts['data'] as $postsData) {
foreach ($postsData as $post) {
$destinationName = $this->destinationService->getDestinationNameById(
$post->getDestinationId()
);
$destinationNames[$post->getId()] = $destinationName;
}
}
Like this, you could send $destinationNames to your template and find the right $destinationName thanks to the index.

How to access Object Element inside an Array without a foreach loop

I'm trying to learn the use of the Zend Framework and I am facing now the following issue.
I am reading some information from the database for a specific Post. I use Datamapper and Models.
$postMapper = new Application_Model_PostMapper();
$post = new Application_Model_Post();
$details = $postMapper->find($postID, $post);
$this->view->postDetail = $details;
In my View, I use a foreach($this->postDetail as $value) to read all the Post Information. But I was wondering now, if I can also access an Information without the foreach. I need just the Email Adress in the Controller and can't see why I would need a foreach. But how would I access this? A Zend_Debug comes with the following results:
array(1) {
[0] => object(Application_Model_Post)#87 (27) {
["_email":protected] => string(10) "test#testmail.com"
It does sound like a very stupid question, but I just don't find a way to read out the Email Adress inside the Controller. Can someone give me a hint?
In your Application_Model_Post class, you would create an accessor method in order to get the private value.
So create a method like this.
public function getEmail(){
return $this->_email;
}
To be honest, I think you will already have these methods if you are using a datamapper correctly.
When you get data from Model/db by fetchAll, eg:
$result = $this->fetchAll($select);
you can
$result->toArray();
//access like array
$result[0]->some_col_1;
$result[0]->some_col_2;
$result[1]->some_col_1;
$result[1]->some_col_2;
...
one of these should work:
if $details returns an array: $email = $details['email']; or =$details[0]['email'];
if $details returns an object: $email = $details->email;

idiorm / paris has_many as_array result set

I'm having trouble getting the results of a has_many query using php idiorm/paris. Following the example from the paris site the has_many result for posts returns as an object.
That's great, and I can run through the object and access individual methods, but what I want to be able to do is pass the result set as an associative array off to my template engine for display.
Example:
class Post extends Model {
}
class User extends Model {
public function posts() {
return $this->has_many('Post'); // Note we use the model name literally - not a pluralised version
}
}
The api works this way:
// Select a particular user from the database
$user = Model::factory('User')->find_one($user_id);
// Find the posts associated with the user
$posts = $user->posts()->find_many();
I am able to access the posts object and print the result set like this:
// echo each post id
foreach ($posts as $post) {
echo $post->id;
}
What I'd really like to be to do though, is use as_array() to get the entire resultset as an associative array, limited by certain fields in the way as_array works for an individual row, e.g.
$post_list = $posts()->as_array(id,title,post,date);
This, or a call on something like $user->posts()->find_many()->as_array() don't work.
What is the correct way to access this type of result set using paris?
Adding this method to idiorm.php gives me the desired functionality.
public function find_array() {
if (func_num_args() === 0) {
return $this->_run();
}
$args = func_get_args();
$array = array();
foreach ($this->_run() as $r) {
$array[] = array_intersect_key($r, array_flip($args));
}
return $array;
}
Now I can call $post_list = $posts()->find_array(); or $posts()->find_array('id','title'); etc.
find_one returns a Model object, find_many returns an array of Models.
If you want to get the entire result set as an array of associative array, one solution should be to use array_map
function model_as_array($model) {
return $model->as_array();
}
$posts = $user->posts()->find_many();
$my_view->posts = array_map(model_as_array, $posts);
var_dump($my_view->posts);
or in php 5.3+ (not tested)
$aa_posts = array_map(function($model) {return $model->as_array();} , $posts);

php Array Initialization

I need to initialize an array of objects in PHP.
Presently I have the following code:
$comment = array();
And when i am adding an element to the array
public function addComment($c){
array_push($this->comment,$c);
}
Here, $c is an object of class Comment.
But when I try to access an functions of that class using $comment, I get the following error:
Fatal error: Call to a member function
getCommentString() on a non-object
Can anyone tell me how to initialize an array of objects in php?
Thanks
Sharmi
$this->comment = array();
Looks like a scope problem to me.
If $comments is a member of a class, calling $comments inside a function of that class will not actually use the member, but rather use an instance of $comments belonging to the scope of the function.
If other words, if you are trying to use a class member, do $this->comments, not just $comments.
class foo
{
private $bar;
function add_to_bar($param)
{
// Adds to a $bar that exists solely inside this
// add_to_bar() function.
$bar[] = $param;
// Adds to a $bar variable that belongs to the
// class, not just the add_to_bar() function.
$this->bar[] = $param;
}
}
This code might help you:
$comments = array();
$comments[] = new ObjectName(); // adds first object to the array
$comments[] = new ObjectName(); // adds second object to the array
// To access the objects you need to use the index of the array
// So you can do this:
echo $comments[0]->getCommentString(); // first object
echo $comments[1]->getCommentString(); // second object
// or loop through them
foreach ($comments as $comment) {
echo $comment->getCommentString();
}
I think your problem is either how you are adding the objects to the array (what is $this->comment referencing to?) or you may be trying to call ->getCommentString() on the array and not on the actual objects in the array.
You can see what's in the array by passing it to print_r():
print_r($comment);
Presuming you have Comment objects in there, you should be able to reference them with $comment[0]->getCommentString().

PHP Object return value

Currently I'm programming a database class which makes a little bit use of PHP's PDO class, but I'd like to add some simple features for making programming a certain application a bit easier.
Now In the following piece of pseudo code you can see where I'm going. The only problem in this example is that the $result variable is an object, which cannot be used for comparisation of some stuff I'm doing further on in the script:
<?php
class Database
{
public function FetchRow ( $query )
{
// .. do some stuff, and make a $result variable
return DatabaseStatement ( $result );
}
}
class DatabaseStatement
{
private $result;
public function __construct ( $query )
{
// .. save result in property etc.
}
public function __get ( $column )
{
// .. check result item
return $this -> result [ $column ];
}
}
$db = new Database;
$result = $db -> Query ( 'SELECT * FROM users WHERE id = 1;' );
if ( $result != null ) // Here $result should be an array OR null in case no rows are returned
{
echo $result -> username; // Here $result should call the __get method
echo '<pre>' , print_r ( $result ) , '</pre>'; // Here $result should be the array, cause it wasn't null just yet
}
As you can see the $result variable should not be an object when I'm doing a comparisation, I know it can be made to a string using __toString. But I'd like it to be some other type, mostly an array or null.
How do I get something like that working if it's possible (should be possible I think with too much hassle)?
So can somebody point me in the right direction, or possibly give a piece of code that should work or I can change to fit in my current class?
It seems to me that you just need to add some methods that do what you want. Instead of forcing the $result object to be an array or null to check whether it's empty, why don't you just create and call a method isEmpty () that tells you what you want to know?
And if you need an array, create a method toArray () that returns what you want. OR, even better, make your object implement Iterator and/or ArrayAccess from the Standard PHP Library.
I think you'll have to do this in the same place you create the DatabaseStatement. So for instance:
public function FetchRow($query)
{
// ... do some stuff, and make a $result variable.
$ds = DatabaseStatement($result);
if ($ds) {
return $ds;
}
else {
return null;
}
}
That's not possible. PHP doesn't allow you to overload operators.
Use the PDOStatment class and it's rowCount property.

Categories