1

I am trying to create a little rental project where I basically currently have a table which shows 5 types of values which are taken from a database (mySQL) and are shown with the help of twig. A MVC structure is applied. Visual representation: enter image description here

Sorry, I blocked the information out because the addresses and whatnot are actually real.

And now to the issue: I'd like to be able to add a customer (and later on edit and remove as well, but first things first.)

I put this in my CustomerModel.php file:

class CustomerModel extends AbstractModel {
  public function addCustomer($customerNumber, $customerName, $customerAddress, $postalAddress, $phoneNumber) {
    $customersQuery = "INSERT INTO Customers(customerNumber, customerName, customerAddress, postalAddress, phoneNumber) " .
                      "VALUES(:customerNumber, :customerName, :customerAddress, :postalAddress, :phoneNumber)";
    $customersStatement = $this->db->prepare($customersQuery);
    $customersStatement->execute(["customerNumber" => $customerNumber,
    "customerName" => $customerName,
    "customerAddress" => $customerAddress,
    "postalAddress" => $postalAddress,
    "phoneNumber" => $phoneNumber]);
    if (!$customersStatement) die("Fatal error.");
    $customerNumber = $this->db->lastInsertId();
    return $customerNumber;
  } 

and this in CustomerController.php

class CustomerController extends AbstractController {
  public function addCustomer() {
    return $this->render("AddCustomer.twig", []);
  }

  public function customerAdded() {
    $form = $this->request->getForm();
    $customerName = $form["customerName"];
    $customerModel = new CustomerModel($this->db);
    // $customerNumber = $customerModel->addCustomer($customerNumber, $customerName, $customerAddress, $postalAddress, $phoneNumber);
    $customerNumber = $customerModel->addCustomer($customerName);
    $customerAddress = $customerModel->addCustomer($customerName);
    $postalAddress = $customerModel->addCustomer($customerName);
    $phoneNumber = $customerModel->addCustomer($customerName);
    $properties = ["customerNumber" => $customerNumber,
                   "customerName" => $customerName,
                  "customerAddress" => $customerAddress,
                "postalAddress" => $postalAddress,
              "phoneNumber" => $phoneNumber];
    return $this->render("CustomerAdded.twig", $properties);
  }   
}  

and now when I try to enter the information in my form, I get the errors:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function Carrental\Models\CustomerModel::addCustomer(), 1 passed in /home/vagrant/code/assignments/XXXX/src/Controllers/CustomerController.php on line 18 and exactly 5 expected in /home/vagrant/code/assignments/XXXX/src/Models/CustomerModel.php on line 11

( ! ) ArgumentCountError: Too few arguments to function Carrental\Models\CustomerModel::addCustomer(), 1 passed in /home/vagrant/code/assignments/XXXX/src/Controllers/CustomerController.php on line 18 and exactly 5 expected in /home/vagrant/code/assignments/XXXX/src/Models/CustomerModel.php on line 11

Clearly, there is something I definitely don't understand with what I have done..

Many thanks in advance.

1
  • That message seems quite clear, your method expects 5 parameters, you only gave it one. And always the same, that is. Commented Jan 5, 2020 at 5:19

1 Answer 1

1

Your method public function addCustomer($customerNumber, $customerName, $customerAddress, $postalAddress, $phoneNumber) expects exactly 5 parameters, but you call it with only one (and it's always the same). Change your customerAdded() to something like this (make sure the $form["XYZ"] fields have the actual names of your form fields):

public function customerAdded() {
    $form = $this->request->getForm();
    $customerName = $form["customerName"];
    $customerNumber = $form["customerNumber"];
    $customerAddress = $form["customerAddress"];
    $postalAddress = $form["postalAddress"];
    $phoneNumber = $form["phoneNumber"];
    $customerModel = new CustomerModel($this->db);
    $customerModel->addCustomer($customerNumber, $customerName, $customerAddress, $postalAddress, $phoneNumber);
    $properties = ["customerNumber" => $customerNumber,
                   "customerName" => $customerName,
                  "customerAddress" => $customerAddress,
                "postalAddress" => $postalAddress,
              "phoneNumber" => $phoneNumber];
    return $this->render("CustomerAdded.twig", $properties);
  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.