-
@oddz: I don't want to bother you anymore. This will be my last post on this topic, I need to find another way for learning this without having to make 3423423 posts. :s I realise that, and I'm really sorry. You explanations however, have been precious for starting on this OOP thing. :)
The script returns no errors. However, all the data inserted on the db table is NULL. I don't know why.
Here is the pages that (I suppose) are the relevant ones):
ProcessINSERTdog.php
PHP Code:
<?php
$id_vet=$_POST["listvets"];
$dogname=$_POST["txt_dogname"];
$dog = new Dog();
$vet = new Vet();
$dog->setVet($vet);
$vet->setId($id_vet);
$dogdao = new DogDAO();
$dogdao->insert($dog);
?>
DogDAO.class.php
PHP Code:
class DogDAO extends DAOGeral {
public function insert(Dog $dog){
$stmt = $this->_dbh->prepare("INSERT INTO DOG (DOG_NAME, ID_VET) VALUES (?, ?)");
$stmt->bindParam(1, $dog->getDogName(), PDO::PARAM_STR );
$stmt->bindParam(2, $dog->getVet()->getId(), PDO::PARAM_INT);
$stmt->execute();
}
}
?>
Dog.class.php
PHP Code:
class Site {
private $_id_dog;
private $_name_dog;
private $_vet;
public function setId( $id_dog ){
$this->_id_dog = $id_dog;
}
public function setDogName( $name_dog ) {
$this->_name_dog = $name_dog;
}
public function setVet(Vet $vet) {
$this->_vet = $vet;
}
public function getId(){
return $this->_id_dog;
}
public function getDogName(){
return $this->_name_dog;
}
public function getVet(){
return $this->_vet;
}
}
?>
Regards,
Márcio
-
Its no bother.
Anyways, the first thing I would go ahead and do is check the expected output.
PHP Code:
class DogDAO extends DAOGeral {
public function insert(Dog $dog)
echo '<p><strong>dog name:</strong> ',$dog->getDogName(),'</p>';
echo '<p><strong>vet id</strong> ',$dog->getVet()->getId(),'</p>';
}
}
?>
Does that yield two paragraphs with the expected data?
If it does then your problem is with the query. If it doesn't then the problem is outside this method.
Also, take a look at this this thread for the proper way to handle errors using prepared statements. Especially, throwing exceptions which will help you bug and error fix your code.
What does the create statement for the DOG table look like? The problem could be your leaving out a required field. In which case the prepare method would fail and if you were using proper error handling could easily identify by throwing an exception when execute or prepare fail.
-
Ok. The problem was that I was not setting my values to insert on the database, anywhere. I was not setting the values. I was not using the set methods created!
:S I'm soooo newbie!!! Gash!!
Now all the values do insert. The only value that does not insert is the id_vet.
I know that this value comes from the POST, I know that the query is ok.
I have corrected this line:
PHP Code:
$vet->setId($idvet);
to this:
PHP Code:
$vet->setId($id_vet);
because this is the proper name of the var that keeps the POST values.
I've also change this:
PHP Code:
$stmt->bindParam(3, $site->getVet()->getId($id_vet), PDO::PARAM_INT);
to this:
PHP Code:
$stmt->bindParam(3, $site->getVet()->getId(), PDO::PARAM_INT);
Since we don't need any params there, right?
And I have change the order here, instead of this:
PHP Code:
$site->setVet($vet);
$vet->setId($id_vet);
I'm having this:
PHP Code:
$vet->setId($id_vet);
$dog->setVet($vet);
I believe its more logical to first set the id of the vet object and then pass that object (already with the value) as a dog property. Yes?
Still, I'm having no luck. And id_vet still appear as NULL.
Any clue? :D
Regards,
Márcio
-
this works:
PHP Code:
class DAOGeral {}
class DogDAO extends DAOGeral {
public function insert(Dog $dog){
echo '<p><strong>Dog Name</strong> ',$dog->getDogName(),'</p>';
echo '<p><strong>Dog Vet:</strong> ',$dog->getVet()->getId(),'</p>';
}
}
class Vet {
private $id;
public function setId($id) {
$this->id = $id;
}
public function getId() {
return $this->id;
}
}
class Dog {
private $_id_dog;
private $_name_dog;
private $_vet;
public function setId( $id_dog ){
$this->_id_dog = $id_dog;
}
public function setDogName( $name_dog ) {
$this->_name_dog = $name_dog;
}
public function setVet(Vet $vet) {
$this->_vet = $vet;
}
public function getId(){
return $this->_id_dog;
}
public function getDogName(){
return $this->_name_dog;
}
public function getVet(){
return $this->_vet;
}
}
$id_vet = 2;
$dogname = 'rusty';
$dog = new Dog();
$vet = new Vet();
$dog->setVet($vet);
$vet->setId($id_vet);
$dog->setDogName($dogname);
$dogdao = new DogDAO();
$dogdao->insert($dog);
Using this case does your code result in the expected result?
PHP Code:
$id_vet = 2;
$dogname = 'rusty';
$dog = new Dog();
$vet = new Vet();
$dog->setVet($vet);
$vet->setId($id_vet);
$dog->setDogName($dogname);
$dogdao = new DogDAO();
$dogdao->insert($dog);
By the way, order doesn't matter. You can pass the vet to the dog then set the vets id because PHP 5 passes by reference if your using PHP 5.
PHP Code:
$vet->setId($id_vet);
$dog->setVet($vet);
or
PHP Code:
$dog->setVet($vet);
$vet->setId($id_vet);
-
Thanks a lot, once again. I will look at your post. Before that... please have a look on this:
I found where the NULL comes out, I believe, because I have done this:
PHP Code:
$vet->setId($id_vet);
var_dump($vet->setId($id_vet));
And here I get the NULL. :D
So the method responsable for the null seems to be:
the setId method of the Vet class.
Here:
PHP Code:
public function setId( $id_vet ){
$this->_id_vet = $id_vet;
}
Any illegality here? Or is normal to get a NULL when I do a var_dump of all this:
$vet->setId($id_vet) ?
Regards,
Márcio
-
PHP Code:
$vet->setId($id_vet);
var_dump($vet->setId($id_vet));
Your going to get NULL here because setId() doesn't return any value.
what about:
PHP Code:
$vet->setId($id_vet);
var_dump($vet->getId());
-
Of course, a set does not return. The get does. Cool. :) I know it's newbie, but now I see all my get methods with a return, and my set methods with no return. Cool.
I have tried the above:
PHP Code:
$vet->setId($id_vet);
var_dump($vet->getId());
I also get NULL.
And my getId as this:
public function getId(){
return $this->_id_vet;
}
:shifty:
-
PHP Code:
echo '<p>',$id_vet,'</p>';
Is the number there on screen?
-
-
:sick::sick::sick::sick::sick::sick::sick::sick::sick::sick::sick::sick:
return $this->_id_Vet;
and the property is _id_vet;
It was a V instead of v.
:sick::sick::sick::sick::sick::sick::sick:
I will give it a try.... :goof:
-
could you post the current vet class.
-
-
:weee::weee:Done!!!:weee::weee:
Thanks to all for the replys.
And the newbie patience award goes to:
oddz :award:
:flippy::flippy:
Regards,
Márcio
-