PHP Designing Interfaces in PHP Introducing Interfaces Implement MySQL RepositoryInterface

David Evans
David Evans
9,796 Points

Stuck on Challenge for Implementing Interfaces

Receiving the following error message when testing.

Doesn't make sense as execute shouldn't be returning a boolean.

Bummer! Uncaught Error: Call to a member function execute() on boolean in sqlRepository.php:17

sqlRepository.php
<?php

class sqlRepository extends PDO implements RepositoryInterface
{
  public function all($entity)
  {
    $db = new PDO("sqlite:".__DIR__."/database.db");
    $query = $db->query("SELECT * FROM " . $entity);
    $data = $query->fetchAll(PDO::FETCH_OBJ);
    return $data;
  }

  public function find($table, $value, $field = 'id')
  {
    $db = new PDO('sqlite:' . __DIR__ . '/database.db');
    $query = $db->prepare("SELECT $field FROM $table WHERE value=:value");
    $query->execute(array(':value' => $value));
    //$result = $query->fetch(PDO::FETCH_ASSOC);
  }

}
David Evans
David Evans
9,796 Points

I noticed that the SQL statement inside the prepare should be:

SELECT * FROM $table WHERE $field=:value

But I still receive the error.

2 Answers

David Evans
David Evans
9,796 Points

Unsure what was causing the error message in the first place, but I have passed the challenge after modifying my code a bit.

This is what I ended up with.

<?php

class sqlRepository extends PDO implements RepositoryInterface
{
  public function all($entity)
  {
    $query = $this->prepare('SELECT * FROM ' . $entity);
    $query->execute();
    $results = $query->fetchAll(PDO::FETCH_OBJ);

    return $results;
  }

  public function find($entity, $id, $field='id')
  {
    $query = $this->prepare('SELECT * FROM ' . $entity . ' WHERE ' . $field . '=:id');
    $query->bindValue(':id', $id);
    $query->execute();
    $results = $query->fetchAll(PDO::FETCH_OBJ);

    return $results;
  }
}

I swapped out the creation of the PDO object and used $this since we are extending the PDO class.

I could have, and probably should have created a variable for the SQL Query outside of the prepare statement and just used that variable inside the prepare method, but meh it works for now.

Jorge Saucedo
Jorge Saucedo
15,284 Points

Haha! You're great man! You're answering your own question. Hey David, did you find this challenge out of order? I felt that I needed to know a lot more than it was given on the videos to be able to do this challenge.

Daryl Peterson
Daryl Peterson
7,811 Points

Since you are extending the PDO object look at the __construct for the PDO object. It make more since to call the parent object __construct and having access to the pdo properties using $this.

<?php class sqlRepository extends PDO implements RepositoryInterface { protected $file;

public function __construct($file)
{

    $this->file = $file;
    $file_name  = str_replace("sqlite:", '', $file);

    if (!file_exists($file_name)) {
        die('Invalid sqlite file: '.$file_name);
    }

    try {
        parent::__construct($file);
    } catch (PDOException $e) {
        die($e->getMessage());
    }
}

public function all($entity)
{


    $sql  = "SELECT * FROM $entity";
    $stmt = $this->query($sql);
    return $stmt->fetchAll(PDO::FETCH_OBJ);
}

public function find($entity, $value, $field = 'id')
{
    $sql  = "SELECT * FROM $entity WHERE $field = :value";
    $stmt = $this->prepare($sql);
    $stmt->bindParam(':value', $value, PDO::PARAM_STR);
    $stmt->execute();
    return $stmt->fetchAll(PDO::FETCH_OBJ);

}

} ?>