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);

}

価格として 10 進数が選択されていると、別のエラーが発生します。以下は、通知コードとコンテキストです。

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