1

好吧,让我从头开始,我是一个新手,正在研究一个用 cakephp 1.2 编码并试图让它工作的发票示例,但我使用的是 Cakephp 2.1 和 php 5.?? 这意味着一些代码已从原始代码更改,我正在尝试找出它们,这个错误已经查看了 3 天但没有得到任何地方,这就是我发送求救信号的原因:)。

私有方法

function __lists ()
{
    $users = $this->Invoice->User->find('list', array('fields'=> array('User.id', 'User.username')));
    $statuses = $this->Invoice->Status->find('list');
    $clients = $this->Invoice->Client->find('list');
    /*$units = $this->Invoice->Item->Unit->find('list');*/
    $this->set('units', $this->Invoice->Item->Unit->find('list'));      
    $categories = $this->Invoice->Category->find('list');
    $this->set(compact('users', 'statuses', 'clients', 'categories','units'));
}


# Tallys up invoice and alters $this->data
# adding total, subTotal and taxTotal
function __tallyUpInvoice()
{
    $this->__itemTotals();
  $subTotal = $this->__subtotal($this->data['Item']);
    $taxTotal = $this->__tax($subTotal, $this->data['Invoice']['taxRate']);
    $this->request->data['Invoice']['total'] = $subTotal + $taxTotal;
    $this->request->data['Invoice']['subTotal'] = $subTotal;
    $this->request->data['Invoice']['taxTotal'] = $taxTotal;
    return true;
}

# returns line item qty times price
function __lineItemTotal($qty, $price)
{
    $arrayjoin = array_merge((array)$qty, (array)$price);
    return array_product($arrayjoin);
    settype($arrayjoin, "integer");
    print_r($arrayjoin);

    /*return array($qty) * array($price);
    /*return array_product($qty) + array_product($price); */
}

# Alters $this->data['Item']['total'] with total of line item.
function __itemTotals()
{
    $itemsCount = isset($this->data['Item']) ? count($this->data['Item']) : 0;
  for ($i=0; $i < $itemsCount; $i++)
  {
    $this->request->data['Item'][$i]['total'] = $this->__lineItemTotal($this->request->data['Item'][$i]['qty'], $this->request->data['Item'][$i]['price']);
        
        // collect unit name from unit model
        $unit = $this->Invoice->Item->Unit->find('first', array('conditions' => array('Unit.id =' => $this->request->data['Item'][$i]['unit_id']), 'fields' => array('Unit.name')));
        $this->request->data['Item'][$i]['name'] = $unit['Unit']['name'];
  }
}

# returns sub total price
function __subTotal($items)
{
    $price = 0.0;

    foreach ($items as $item) 
    {
       $price += $this->__lineItemTotal($item['qty'], $item['price']);
    }
  return $price;
}


 # returns total tax amount
function __tax($subTotal, $taxRate)
{
$taxTotal = round(($taxRate * 0.01 * $subTotal) * 1000) / 1000;
    return round($taxTotal * 100) / 100; 
}   

# attaches Invoice.id to $this->data['Item']
function __attachInvoiceIdToItems($id)
{
    $itemsCount = isset($this->request->data['Item']) ? count($this->request-    >data['Item']) : 0;
  for ($i=0; $i < $itemsCount; $i++)
  {
    $this->request->data['Item'][$i]['invoice_id'] = $id;
  }
}

上面的代码片段与我正在处理的代码部分有关,从头开始。 这是原始代码

# returns line item qty times price
function __lineItemTotal($qty, $price)
{
    
    return $qty * $price;
}

阅读此处后,我收到一个错误操作数错误,这是我试图用一个字符串进行乘法和数组运算,或者这就是我的想法,所以我将其更改为下面的函数

# returns line item qty times price
function __lineItemTotal($qty, $price)
{
    
$arrayjoin = array_merge((array)$qty, (array)$price);
    return array_product($arrayjoin);
    settype($arrayjoin, "integer");
    print_r($arrayjoin);

}

Now I am getting another error when there is a decimal number selected as a price, below is the notice code and context.

Notice (8): Array to string conversion [CORE/Cake/Model/Datasource/DboSource.php, line 1006]
 implode - [internal], line ??
DboSource::create() - CORE/Cake/Model/Datasource/DboSource.php, line 1006
Model::save() - CORE/Cake/Model/Model.php, line 1730
Model::saveMany() - CORE/Cake/Model/Model.php, line 2089
Model::saveAll() - CORE/Cake/Model/Model.php, line 2026
InvoicesController::add() - APP/Controller/InvoicesController.php, line 128
ReflectionMethod::invokeArgs() - [internal], line ??
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 485
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 186
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 161
[main] - APP/webroot/index.php, line 92

代码:

        'table' => $this->fullTableName($model),
        'fields' => implode(', ', $fieldInsert),
        'values' => implode(', ', $valueInsert)

语境:

$model = object(Item) {
displayField => 'name'
belongsTo => array(
    'Invoice' => array(
        [maximum depth reached]

    ),
    'Unit' => array(
        [maximum depth reached]
    )
)
useDbConfig => 'default'
useTable => 'items'
id => false
data => array(
    'Item' => array(
        [maximum depth reached]
    )
)
schemaName => 'invoicr'
table => 'items'
primaryKey => 'id'
validate => array()
validationErrors => array()
validationDomain => null
name => 'Item'
alias => 'Item'
tableToModel => array(
    'items' => 'Item',
    'units' => 'Unit'
)
cacheQueries => false
hasOne => array()
hasMany => array()
hasAndBelongsToMany => array()
actsAs => null
Behaviors => object(BehaviorCollection) {}
whitelist => array()
cacheSources => true
findQueryType => null
recursive => (int) 1
order => null
virtualFields => array()
__backAssociation => array()
__backInnerAssociation => array()
__backOriginalAssociation => array()
__backContainableAssociation => array()
findMethods => array(
    'all' => true,
    'first' => true,
    'count' => true,
    'neighbors' => true,
    'list' => true,
    'threaded' => true
)
Unit => object(Unit) {}
tablePrefix => ''

$fields = array(
(int) 0 => 'unit_id',
(int) 1 => 'qty',
(int) 2 => 'description',
(int) 3 => 'price',
(int) 4 => 'total',
(int) 5 => 'name',
(int) 6 => 'invoice_id'
)

$values = array(
(int) 0 => '1',
(int) 1 => array(
    (int) 0 => '55.00'
),
(int) 2 => array(
    (int) 0 => ' testing ="testing "'
),
(int) 3 => array(
    (int) 0 => '10.01'
),
(int) 4 => (float) 550.55,
(int) 5 => 'Service',
(int) 6 => '49'
)
$id = null
$count = (int) 7
$i = (int) 7
$valueInsert = array(
(int) 0 => '1',
(int) 1 => array(
    (int) 0 => '55.00'
),
(int) 2 => array(
    (int) 0 => '' testing =\"testing \"''
),
(int) 3 => array(
    (int) 0 => '10.01'
),
(int) 4 => '550.55',
(int) 5 => ''Service'',
(int) 6 => '49'
)
$fieldInsert = array(
(int) 0 => '`unit_id`',
(int) 1 => '`qty`',
(int) 2 => '`description`',
(int) 3 => '`price`',
(int) 4 => '`total`',
(int) 5 => '`name`',
(int) 6 => '`invoice_id`'
 )

 }

看了几个星期后,我发现保存它 mysql 正在尝试保存一个数组,那个设置在哪里?

 Code:
 $valueInsert = array(
 (int) 0 => '1',
 (int) 1 => array(
 (int) 0 => '55.00'
 ),
 (int) 2 => array(
 (int) 0 => '' testing =\"testing \"''
  ),
   (int) 3 => array(
 (int) 0 => '10.01'
 ),
 (int) 4 => '550.55',
 (int) 5 => ''Service'',
 (int) 6 => '49'
 )

谢谢大家希望可以解决这个问题。

干杯

萨基

4

0 回答 0