0

The context :

I'm using php langage in order to develop a new module on Dolibarr software and I need your help because I don't overcome to solve my issue since 2 days.

The process :

I created a very simple php form with only 2 fields : lastname ("nom" in French) and firstname ("prénom" in French). I fill my form, then I submit it and I should have my object saved into my MySQL database.

The issue :

I'm getting a blank page and none object is saved into my database.

Files :

The first one according to the new table :

#llx_moduletest_myobject.sql 

CREATE TABLE llx_moduletest_myobject(
    -- BEGIN MODULEBUILDER FIELDS
    rowid INTEGER AUTO_INCREMENT PRIMARY KEY,
    nom VARCHAR(255) NOT NULL,
    prenom VARCHAR(255) NOT NULL
    -- END MODULEBUILDER FIELDS
) ENGINE=innodb;

The second one corresponds to the action and the view :

#card.php

<?php

// Load traductions files requiredby by page
$langs->loadLangs(array("moduletest@moduletest","other"));

// Get parameters
$id         = GETPOST('id', 'int');
$ref        = GETPOST('ref', 'alpha');
$action     = GETPOST('action', 'alpha');
$cancel     = GETPOST('cancel', 'aZ09');
$backtopage = GETPOST('backtopage', 'alpha');

// Initialize technical objects
$object=new MyObject($db);

/*
 * Actions
 *
 * Put here all code to do according to value of "action" parameter
 */

// If create a request
if ($action == 'create')
{
        $object = new FormFile($db);

        $db->begin();

            $nom = GETPOST('nom');
            $prenom = GETPOST('prenom');

        // If no name
            if (empty($nom))
            {
                setEventMessages($langs->trans("Pas de nom"), null, 'errors');
                $error++;
                $action='create';
            }

        // If no firstname
            if (empty($prenom))
            {
                setEventMessages($langs->trans("Pas de prénom"), null, 'errors');
                $error++;
                $action='create';
            }

            $result = 0;

            if (! $error)
            {
                $object->nom = $nom;
                $object->prenom = $prenom;
                $result = $object->create($user); //return int

             if ($result <= 0)
                {
                        setEventMessages($object->errors, 'errors');
                        $error++;
                        }        
                }

            // If no SQL error we redirect to the request card
            if (! $error)
            {
                print $db->commit();
                $db->commit();

                header("Location: ".$_SERVER['PHP_SELF'].'?id='.$id);
                exit;
            }
            else
                {
                $db->rollback();
            }
}

/* ************************ 
 View
 *********************** */

$form=new Form($db);
$formfile=new FormFile($db);
llxHeader('', $langs->trans('Formulaire de test'));


// Part to create
if ($action == 'create')
{
    // Formulaire de test
        print load_fiche_titre($langs->trans('Formulaire de test'), '', 'title_hrm.png');

    // Formulaire de test
        print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" onsubmit="return valider()" name="Formulaire de test">'."\n";
        print '<input type="hidden" name="action" value="create" />'."\n";

        dol_fiche_head();

        print '<table class="border" width="100%">';
        print '<tbody>';

    // Nom
        print '<tr>';
        print '<td class="fieldrequired">'.$langs->trans("Nom").'</td>';
        print '<td>';
        print '<input type="text" name="nom" value="'.$object->nom.'">';
        print '</td></tr>';

    // Prenom
        print '<tr>';
        print '<td class="fieldrequired">'.$langs->trans("Prenom").'</td>';
        print '<td>';
        print '<input type="text" name="prenom" value="'.$object->prenom.'">';
        print '</td></tr>';

        print '</tbody>';
        print '</table>'; 

        dol_fiche_end();

        print '<div class="center">';
        print '<input type="submit" value="'.$langs->trans("Valider le formulaire").'" name="bouton" class="button">';
        print '&nbsp; &nbsp; ';
        print '<input type="button" value="'.$langs->trans("Cancel").'" class="button" onclick="history.go(-1)">';
        print '</div>';

        print '</form>'."\n";
}
// End of page
llxFooter();
$db->close();

I have a create() function : $result = $object->create($user); //return int

This function lets to create object into my database :

#moduletestmyobject.class.php

function create($user, $notrigger=0)
    {
        global $conf, $langs;
        $error=0;
        // Clean parameters
        if (isset($this->prop1)) $this->prop1=trim($this->prop1);
        if (isset($this->prop2)) $this->prop2=trim($this->prop2);
        // Check parameters
        // Put here code to add control on parameters values
        // Insert request
        $sql = "INSERT INTO ".MAIN_DB_PREFIX."moduletest_myobject(";
        $sql.= " field1,";
        $sql.= " field2";
        $sql.= ") VALUES (";
        $sql.= " '".$this->prop1."',";
        $sql.= " '".$this->prop2."'";
        $sql.= ")";
        $this->db->begin();
        dol_syslog(get_class($this)."::create sql=".$sql, LOG_DEBUG);
        $resql=$this->db->query($sql);
        if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
        if (! $error)
        {
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."moduletest_myobject");

        }
        // Commit or rollback
        if ($error)
        {
            foreach($this->errors as $errmsg)
            {
                dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
                $this->error.=($this->error?', '.$errmsg:$errmsg);
            }
            $this->db->rollback();
            return -1*$error;
        }
        else
        {
            $this->db->commit();
            return $this->id;
        }
    }

I'm getting this issue in apache2 logs :

Uncaught Error: Call to undefined method FormFile::create()
Essex
  • 6,042
  • 11
  • 67
  • 139
  • https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php/12772851#12772851 turn on your error reporting – Tschallacka Nov 02 '17 at 15:15
  • @Tschallacka I edited my question thanks to your answer – Essex Nov 02 '17 at 15:25
  • in your FormFile object, is the function create public? – Tschallacka Nov 02 '17 at 15:32
  • Yes it's a public function – Essex Nov 02 '17 at 15:38
  • show your FormFile object definition. it says it doesn't have a create function. You only showed the create function of the MyObject. But in the create action, you overwrite $object with `$object = new FormFile($db);` so there's an issue there. Your `myobject` gets overwritten by FormFile. – Tschallacka Nov 02 '17 at 15:41
  • I checked in `html.formfile.class.php` and there is none function `create`. All functions according to CRUD are stored in `moduletestmyobject.class.php`. So I have to remove all FormFile overwritten ? Unfortunately I'm new with PHP and Dolibarr. – Essex Nov 02 '17 at 15:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158085/discussion-between-tschallacka-and-deadpool). – Tschallacka Nov 02 '17 at 15:48

2 Answers2

0

I'm getting blank page it's because you check if action == "create" or action when loading the page does not have a default value.

For issue : Uncaught Error: Call to undefined method FormFile::create() -> replaces $object = new FormFile($db); by $object=new MyObject($db);

Tndiaye
  • 1
  • 2
0

First of all, you must load the dolibarr environment at the beginning of the php page

// Load Dolibarr environment
$res=0;
// Try main.inc.php into web root known defined into CONTEXT_DOCUMENT_ROOT (not always defined)
if (! $res && ! empty($_SERVER["CONTEXT_DOCUMENT_ROOT"])) $res=@include($_SERVER["CONTEXT_DOCUMENT_ROOT"]."/main.inc.php");
// Try main.inc.php into web root detected using web root calculated from SCRIPT_FILENAME
$tmp=empty($_SERVER['SCRIPT_FILENAME'])?'':$_SERVER['SCRIPT_FILENAME'];$tmp2=realpath(__FILE__); $i=strlen($tmp)-1; $j=strlen($tmp2)-1;
while($i > 0 && $j > 0 && isset($tmp[$i]) && isset($tmp2[$j]) && $tmp[$i]==$tmp2[$j]) { $i--; $j--; }
if (! $res && $i > 0 && file_exists(substr($tmp, 0, ($i+1))."/main.inc.php")) $res=@include(substr($tmp, 0, ($i+1))."/main.inc.php");
if (! $res && $i > 0 && file_exists(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php")) $res=@include(dirname(substr($tmp, 0, ($i+1)))."/main.inc.php");
// Try main.inc.php using relative path
if (! $res && file_exists("../main.inc.php")) $res=@include("../main.inc.php");
if (! $res && file_exists("../../main.inc.php")) $res=@include("../../main.inc.php");
if (! $res && file_exists("../../../main.inc.php")) $res=@include("../../../main.inc.php");
if (! $res) die("Include of main fails");

include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php');
include_once(DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php');
pushkin
  • 9,575
  • 15
  • 51
  • 95
Razmi Martinez
  • 115
  • 2
  • 11