I'm trying to get all the collection from a database in mongodb with PHP but I didn't find anything on internet useful.
This is what I get so far
$client = new MongoDB\client;
$database = $client->database2;
$collections = $database->listCollections();
foreach ($collections as $collection) {
echo $collection;
}
I'm using listCollections() but doesn't work. I just need to get all data to show it.
This is my folder structure if it helps
This is the output:
Recoverable fatal error: Object of class MongoDB\Model\CollectionInfo could not be converted to string
Finally, I could do it with getName() to get all the collections name from my database.
$colecciones = $database->listCollections();
foreach ($colecciones as $col) {
echo $col->getName();
}
Your result is an object and you try to convert them to a string with an echo. So use var_dump() for example to make the object visible. Then you can use the methods to get more informations from the collection.
One of the methods is getName() you can use them to get the name of the collection.
MongoDBModelCollectionInfo
Related
I have the following class with several properties and a method in PHP (This is simplified code).
class Member{
public $Name;
public $Family;
public function Fetch_Name(){
for($i=0;$i<10;$i++){
$this[$i]->$Name = I find the name using RegExp and return the value to be stored here;
$this[$i]->Family = I find the family using RegExp and return the value to be stored here;
}
}//function
}//class
In the function Fetch_Name(), I want to find all the names and families that is in a text file using RegExp and store them as properties of object in the form of an array. But I don't know how should I define an array of the Member. Is it logical or I should define StdClass or 2-dimension array instead of class?
I found slightly similar discussion here, but a 2 dimensional array is used instead of storing data in the object using class properties.
I think my problem is in defining the following lines of code.
$Member = new Member();
$Member->Fetch_name();
The member that I have defined is not an array. If I do define it array, still it does not work. I did this
$Member[]= new Member();
But it gives error
Fatal error: Call to a member function Fetch_name() on a non-object in
if I give $Member[0]= new Member() then I don't know how to make $Member1 or Member[2] or so forth in the Fetch_Name function. I hope my question is not complex and illogical.
Many thanks in advance
A Member object represents one member. You're trying to overload it to represent or handle many members, which doesn't really make sense. In the end you'll want to end up with an array that holds many Member instances, not the other way around:
$members = array();
for (...) {
$members[] = new Member($name, $family);
}
Most likely you don't really need your Member class to do anything really; the extraction logic should reside outside of the Member class, perhaps in an Extractor class or something similar. From the outside, your code should likely look like this:
$parser = new TextFileParser('my_file.txt');
$members = $parser->extractMembers();
I think you should have two classes :
The first one, Fetcher (or call it as you like), with your function.
The second one, Member, with the properties Name and Family.
It is not the job of a Member to fetch in your text, that's why I would make another class.
In your function, do your job, and in the loop, do this :
for($i = 0; $i < 10; ++$i){
$member = new Member();
$member->setName($name);
$member->setFamily($family);
// The following is an example, do what you want with the generated Member
$this->members[$i] = $member;
}
The problem here is that you are not using the object of type Member as array correctly. The correct format of your code would be:
class Member{
public $Name;
public $Family;
public function Fetch_Name(){
for($i=0;$i<10;$i++){
$this->Name[$i] = 'I find the name using RegExp and return the value to be stored here';
$this->Family[$i] = 'I find the family using RegExp and return the value to be stored here';
}
}
}
First, $this->Name not $this->$Name because Name is already declared as a member variable and $this->Name[$i] is the correct syntax because $this reference to the current object, it cannot be converted to array, as itself. The array must be contained in the member variable.
L.E: I might add that You are not writing your code according to PHP naming standards. This does not affect your functionality, but it is good practice to write your code in the standard way. After all, there is a purpose of having a standard.
Here you have a guide on how to do that.
And I would write your code like this:
class Member{
public $name;
public $family;
public function fetchName(){
for($i=0;$i<10;$i++){
$this->name[$i] = 'I find the name using RegExp and return the value to be stored here';
$this->family[$i] = 'I find the family using RegExp and return the value to be stored here';
}
}
}
L.E2: Seeing what you comented above, I will modify my answer like this:
So you are saying that you have an object of which values must be stored into an array, after the call. Well, after is the key word here:
Initialize your object var:
$member = new Memeber();
$memebr->fechNames();
Initialize and array in foreach
$Member = new Member();
foreach ($Member->Name as $member_name){
$array['names'][] = $member_name;
}
foreach ($Member->Family as $member_family) {
$array['family'][] = $member_family;
}
var_dump($array);
Is this more of what you wanted?
Hope it helps!
Keep on coding!
Ares.
Codeigniter can return a database query as generic "Object" like:
$q = $this->db->get("some_table");
$obj = $this->q->row();
$var = $obj->some_property
In my case I want to make a PHP class who's public variables are 1 for 1 with the database columns, along with some public methods. Is there a quick one-shot way to cast or convert the generic "Row" object into my custom class object? I've read posts that hint that it is certainly possible, but most involve a really hacky serialize/deserialize solution. In the past I have just done:
public function __construct($row) {
$this->prop = $row->prop;
$this->id = $row->id;
$this->value = $row->value;
}
And I find this is very tedious and makes ugly code.
See the third section under result():
CodeIgniter User Guide: Generating Query Results
You can also pass a string to result() which represents a class to instantiate for each result object (note: this class must be loaded)
$query = $this->db->query("SELECT * FROM users;");
foreach ($query->result('User') as $row)
{
echo $row->name; // call attributes
echo $row->reverse_name(); // or methods defined on the 'User' class
}
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;
Right now, I have this code where $obj_arr maybe contain array and an object.
$obj_temp = array ($obj_identity, $arr_user, $obj_locale, $arr_query);
foreach ($obj_temp as $maybe_arr) {
if (is_array($maybe_arr)) :
$name = (string) key($maybe_arr);
if (is_object($maybe_arr)) :
???? // how to retrieve a class name ?
endif;
$obj_arr[$name] = $maybe_arr;
}
obj_will_be_extract($obj_arr);
function obj_will_be_extract($obj_arr) {
extract($obj_arr);
//Do the rest
}
I need to create an array consist of combination of objects and arrays. Cause I need to extract it, then how to get an object name?
Use get_class to get the class name of an object.
Since PHP 5.5 you can use MyAction::class statement, so you can get a class name without class 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().